Skip to content
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
7 changes: 5 additions & 2 deletions src/features/s3/presignUrl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { parseUrl } from "@aws-sdk/url-parser";
import { Hash } from "@aws-sdk/hash-node";
import { formatUrl } from "@aws-sdk/util-format-url";

export const getPresignedUrl = async (url: string): Promise<string> => {
const expirationSeconds = 1200; // 20 minutes
// default expiration is 20 minutes (1200 seconds)
export const getPresignedUrl = async (
url: string,
expirationSeconds: number = 1200
): Promise<string> => {
const s3ObjectUrl = parseUrl(url);
const presigner = new S3RequestPresigner({
credentials: {
Expand Down
5 changes: 5 additions & 0 deletions src/reference/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13533,6 +13533,7 @@ paths:
- id
- url
- mimetype
- presigned_url
properties:
id:
type: number
Expand All @@ -13546,6 +13547,10 @@ paths:
type: string
x-stoplight:
id: 6tqj2cg96280v
presigned_url:
type: string
x-stoplight:
id: 8zt5euhrtitz3
cost:
type: number
x-stoplight:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import request from "supertest";
import app from "@src/app";
import { tryber } from "@src/features/database";
import { getPresignedUrl } from "@src/features/s3/presignUrl";

jest.mock("@src/features/s3/presignUrl", () => {
return {
getPresignedUrl: jest
.fn()
.mockImplementation((url: string) => Promise.resolve(url)),
};
});

describe("GET /campaigns/campaignId/finance/otherCosts", () => {
afterEach(() => {
jest.clearAllMocks();
});
beforeAll(async () => {
await tryber.tables.WpAppqEvdProfile.do().insert([
{
Expand Down Expand Up @@ -176,11 +188,13 @@ describe("GET /campaigns/campaignId/finance/otherCosts", () => {
id: 1,
url: "https://example.com/attachment1.pdf",
mimetype: "application/pdf",
presigned_url: expect.any(String),
}),
expect.objectContaining({
id: 2,
url: "https://example.com/attachment2.jpg",
mimetype: "image/jpeg",
presigned_url: expect.any(String),
}),
]),
}),
Expand All @@ -201,6 +215,7 @@ describe("GET /campaigns/campaignId/finance/otherCosts", () => {
id: 3,
url: "https://example.com/attachment3.png",
mimetype: "image/png",
presigned_url: expect.any(String),
}),
]),
}),
Expand Down Expand Up @@ -230,6 +245,20 @@ describe("GET /campaigns/campaignId/finance/otherCosts", () => {
id: 1,
},
description: "Cost 1 description",
attachments: expect.arrayContaining([
expect.objectContaining({
id: 1,
url: "https://example.com/attachment1.pdf",
mimetype: "application/pdf",
presigned_url: expect.any(String),
}),
expect.objectContaining({
id: 2,
url: "https://example.com/attachment2.jpg",
mimetype: "image/jpeg",
presigned_url: expect.any(String),
}),
]),
}),
expect.objectContaining({
cost_id: 2,
Expand All @@ -243,6 +272,14 @@ describe("GET /campaigns/campaignId/finance/otherCosts", () => {
id: 2,
},
description: "Cost 2 description",
attachments: expect.arrayContaining([
expect.objectContaining({
id: 3,
url: "https://example.com/attachment3.png",
mimetype: "image/png",
presigned_url: expect.any(String),
}),
]),
}),
]),
})
Expand Down Expand Up @@ -307,4 +344,24 @@ describe("GET /campaigns/campaignId/finance/otherCosts", () => {
expect(costWithoutAttachments.cost).toBe(50);
expect(costWithoutAttachments.attachments).toEqual([]);
});

it("Should call getPresignedUrl for each attachment", async () => {
await request(app)
.get("/campaigns/1/finance/otherCosts")
.set("Authorization", "Bearer admin");

expect(getPresignedUrl).toHaveBeenCalledTimes(3);
expect(getPresignedUrl).toHaveBeenCalledWith(
"https://example.com/attachment1.pdf",
10800
);
expect(getPresignedUrl).toHaveBeenCalledWith(
"https://example.com/attachment2.jpg",
10800
);
expect(getPresignedUrl).toHaveBeenCalledWith(
"https://example.com/attachment3.png",
10800
);
});
});
72 changes: 46 additions & 26 deletions src/routes/campaigns/campaignId/finance/otherCosts/_get/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import CampaignRoute from "@src/features/routes/CampaignRoute";
import { tryber } from "@src/features/database";
import OpenapiError from "@src/features/OpenapiError";
import { getPresignedUrl } from "@src/features/s3/presignUrl";

type OtherCost = {
cost_id: number;
Expand All @@ -20,6 +21,7 @@ type OtherCost = {
id: number;
url: string;
mimetype: string;
presigned_url: string;
}[];
};

Expand Down Expand Up @@ -77,31 +79,49 @@ export default class OtherCostsRoute extends CampaignRoute<{
.select("id", "url", "mime_type", "cost_id")
.whereIn("cost_id", costIds);

return costs.map((cost) => {
const type = types.find((t) => t.id === cost.type_id);
const supplier = suppliers.find((s) => s.id === cost.supplier_id);
const costAttachments = attachments.filter(
(a) => a.cost_id === cost.cost_id
);

return {
cost_id: cost.cost_id,
cost: cost.cost,
type: {
name: type?.name || "",
id: type?.id || 0,
},
supplier: {
name: supplier?.name || "",
id: supplier?.id || 0,
},
description: cost.description,
attachments: costAttachments.map((a) => ({
id: a.id,
url: a.url,
mimetype: a.mime_type,
})),
};
});
return Promise.all(
costs.map(async (cost) => {
const type = types.find((t) => t.id === cost.type_id);
const supplier = suppliers.find((s) => s.id === cost.supplier_id);
const costAttachments = attachments.filter(
(a) => a.cost_id === cost.cost_id
);

const resolvedAttachments = await Promise.all(
costAttachments.map(async (a) => {
let presignedUrl = a.url;
try {
presignedUrl = await getPresignedUrl(a.url, 10800);
} catch (error) {
console.error(
`Failed to generate presigned URL for ${a.url}:`,
error
);
}
return {
id: a.id,
url: a.url,
mimetype: a.mime_type,
presigned_url: presignedUrl,
};
})
);

return {
cost_id: cost.cost_id,
cost: cost.cost,
type: {
name: type?.name || "",
id: type?.id || 0,
},
supplier: {
name: supplier?.name || "",
id: supplier?.id || 0,
},
description: cost.description,
attachments: resolvedAttachments,
};
})
);
}
}
1 change: 1 addition & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5654,6 +5654,7 @@ export interface operations {
id: number;
url: string;
mimetype: string;
presigned_url: string;
}[];
cost: number;
}[];
Expand Down
Loading