diff --git a/src/db/dal/userSets.ts b/src/db/dal/userSets.ts index 4a3735f..663bc30 100644 --- a/src/db/dal/userSets.ts +++ b/src/db/dal/userSets.ts @@ -18,7 +18,21 @@ export const getById = async (keycloak_id: string, id: string): Promise => { + const filter = await UserSetModel.findOne({ + where: { + [Op.and]: [{ id }, { sharedpublicly: true }], + }, + }); + + if (!filter) { + throw createHttpError(StatusCodes.NOT_FOUND, `User Set #${id} does not exist.`); } return filter; @@ -60,3 +74,19 @@ export const destroy = async (keycloak_id: string, id: string): Promise }); return !!deletedCount; }; + +export const share = async (id: string, keycloak_id: string): Promise => { + const updatedCount = await UserSetModel.update( + { + sharedpublicly: true, + updated_date: new Date(), + }, + { + where: { + [Op.and]: [{ keycloak_id }, { id }], + }, + }, + ); + + return !!updatedCount?.[0]; +}; diff --git a/src/routes/userSets.ts b/src/routes/userSets.ts index 63dcbdc..231ecce 100644 --- a/src/routes/userSets.ts +++ b/src/routes/userSets.ts @@ -1,7 +1,7 @@ import { Router } from 'express'; import { StatusCodes } from 'http-status-codes'; -import { create, destroy, getAll, getById, update } from '../db/dal/userSets'; +import { create, destroy, getAll, getById, getByIdAndShared, share, update } from '../db/dal/userSets'; const userSetsRouter = Router(); @@ -55,4 +55,23 @@ userSetsRouter.delete('/:id', async (req, res, next) => { } }); +userSetsRouter.get('/shared/:id', async (req, res, next) => { + try { + const result = await getByIdAndShared(req.params.id); + res.status(StatusCodes.OK).send(result); + } catch (e) { + next(e); + } +}); + +userSetsRouter.put('/shared/:id', async (req, res, next) => { + try { + const keycloak_id = req['kauth']?.grant?.access_token?.content?.sub; + const result = await share(req.params.id, keycloak_id); + res.status(StatusCodes.OK).send(result); + } catch (e) { + next(e); + } +}); + export default userSetsRouter;