Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: SJIP-572 add shared endpoint to retrieve a set from another user #62

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/db/dal/userSets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,21 @@ export const getById = async (keycloak_id: string, id: string): Promise<IUserSet
});

if (!filter) {
throw createHttpError(StatusCodes.NOT_FOUND, `Saved filter #${id} does not exist.`);
throw createHttpError(StatusCodes.NOT_FOUND, `User Set #${id} does not exist`);
}

return filter;
};

export const getByIdAndShared = async (id: string): Promise<IUserSetsOutput> => {
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;
Expand Down Expand Up @@ -60,3 +74,19 @@ export const destroy = async (keycloak_id: string, id: string): Promise<boolean>
});
return !!deletedCount;
};

export const share = async (id: string, keycloak_id: string): Promise<boolean> => {
const updatedCount = await UserSetModel.update(
{
sharedpublicly: true,
updated_date: new Date(),
},
{
where: {
[Op.and]: [{ keycloak_id }, { id }],
},
},
);

return !!updatedCount?.[0];
};
21 changes: 20 additions & 1 deletion src/routes/userSets.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -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;