Skip to content

Commit

Permalink
added batch download, needs testing
Browse files Browse the repository at this point in the history
  • Loading branch information
AydanPirani committed Jul 5, 2024
1 parent bda01be commit 2504dd8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
36 changes: 34 additions & 2 deletions src/services/s3/s3-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Role } from "../auth/auth-models";

import { S3 } from "@aws-sdk/client-s3";
import { getResumeUrl, postResumeUrl } from "./s3-utils";
import BatchResumeDownloadValidator from "./s3-schema";

const s3Router: Router = Router();

Expand Down Expand Up @@ -37,7 +38,7 @@ s3Router.get(
const userId = payload.userId;

const s3 = res.locals.s3 as S3;

try {
const downloadUrl = await getResumeUrl(userId, s3);
return res.status(StatusCodes.OK).send({ url: downloadUrl });
Expand All @@ -48,7 +49,7 @@ s3Router.get(
);

s3Router.get(
"/download/:USERID",
"/download/user/:USERID",
RoleChecker([Role.Enum.STAFF, Role.Enum.CORPORATE], false),
s3ClientMiddleware,
async (req, res, next) => {
Expand All @@ -64,4 +65,35 @@ s3Router.get(
}
);

s3Router.get(
"/download/batch/:USERID",
RoleChecker([Role.Enum.STAFF, Role.Enum.CORPORATE], false),
s3ClientMiddleware,
async (req, res, next) => {
const s3 = res.locals.s3 as S3;

try {
const { userIds } = BatchResumeDownloadValidator.parse(req.body);

const batchDownloadPromises = userIds.map((userId) =>
getResumeUrl(userId, s3)
);

Check failure on line 81 in src/services/s3/s3-router.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `············`
const batchDownloadUrls = await Promise.allSettled(
batchDownloadPromises
);

const filteredUrls = batchDownloadUrls.forEach((result) => {
if (result.status === "fulfilled") {
return result.value;
}
});

return res.status(StatusCodes.OK).send({ url: filteredUrls });
} catch (error) {
next(error);
}
}
);

export default s3Router;
7 changes: 7 additions & 0 deletions src/services/s3/s3-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { z } from "zod";

const BatchResumeDownloadValidator = z.object({
userIds: z.string().array(),
});

export default BatchResumeDownloadValidator;
2 changes: 1 addition & 1 deletion src/services/s3/s3-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function getResumeUrl(userId: string, client: S3) {
Bucket: Config.S3_BUCKET_NAME,
Key: `${userId}.pdf`,
});

return getSignedUrl(client, command, {
expiresIn: Config.RESUME_URL_EXPIRY_SECONDS,
});
Expand Down

0 comments on commit 2504dd8

Please sign in to comment.