From bb5a7833185eb75e9a417c33e0d77875ac862f3f Mon Sep 17 00:00:00 2001 From: Quan Tran Date: Mon, 18 Dec 2023 01:47:29 +0700 Subject: [PATCH] Refactor API --- packages/core/src/entity/Capture.ts | 9 ++++ packages/core/src/entity/RecursiveCapture.ts | 7 ++- .../functions/src/{ => api}/capture/get.ts | 0 .../functions/src/{ => api}/capture/getAll.ts | 0 .../functions/src/{ => api}/capture/post.ts | 0 .../src/{ => api}/recurringCapture/get.ts | 0 .../src/{ => api}/recurringCapture/post.ts | 4 +- .../src/{ => api}/recurringCapture/put.ts | 0 .../src/triggers/recurringHandler.ts | 47 +++++++++++++++---- stacks/MyStack.ts | 27 ++++++----- 10 files changed, 71 insertions(+), 23 deletions(-) rename packages/functions/src/{ => api}/capture/get.ts (100%) rename packages/functions/src/{ => api}/capture/getAll.ts (100%) rename packages/functions/src/{ => api}/capture/post.ts (100%) rename packages/functions/src/{ => api}/recurringCapture/get.ts (100%) rename packages/functions/src/{ => api}/recurringCapture/post.ts (98%) rename packages/functions/src/{ => api}/recurringCapture/put.ts (100%) diff --git a/packages/core/src/entity/Capture.ts b/packages/core/src/entity/Capture.ts index 5621d73..8d8fba4 100644 --- a/packages/core/src/entity/Capture.ts +++ b/packages/core/src/entity/Capture.ts @@ -8,6 +8,7 @@ import { import { BaseEntityCustom } from "./BaseEntityCustom"; import { Format, Status } from "../constants"; import { User } from "./User"; +import { RecursiveCapture } from "./RecursiveCapture"; @Entity() export class Capture extends BaseEntityCustom { @@ -54,4 +55,12 @@ export class Capture extends BaseEntityCustom { @Column({ type: "varchar", nullable: true }) ownerId?: string; + + // recursive capture ManyToOne + @ManyToOne(() => RecursiveCapture, (recursiveCapture) => recursiveCapture.captures, { nullable: true }) + @JoinColumn({ name: "recursiveCaptureId" }) // this is the column that will hold the foreign key + recursiveCapture?: RecursiveCapture; + + @Column({ type: "varchar", nullable: true }) + recursiveCaptureId: string; } diff --git a/packages/core/src/entity/RecursiveCapture.ts b/packages/core/src/entity/RecursiveCapture.ts index d4d8336..6f9a6f3 100644 --- a/packages/core/src/entity/RecursiveCapture.ts +++ b/packages/core/src/entity/RecursiveCapture.ts @@ -1,7 +1,8 @@ -import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from "typeorm"; +import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, OneToMany } from "typeorm"; import { BaseEntityCustom } from "./BaseEntityCustom"; import { Format } from "../constants"; import { User } from "./User"; +import { Capture } from "./Capture"; @Entity() export class RecursiveCapture extends BaseEntityCustom { @@ -48,4 +49,8 @@ export class RecursiveCapture extends BaseEntityCustom { // ScheduleArn @Column({ type: "varchar", nullable: true }) scheduleArn: string; + + // captures OneToMany one recursive capture can have many captures + @OneToMany(() => Capture, (capture) => capture.recursiveCapture) + captures: Capture[]; } diff --git a/packages/functions/src/capture/get.ts b/packages/functions/src/api/capture/get.ts similarity index 100% rename from packages/functions/src/capture/get.ts rename to packages/functions/src/api/capture/get.ts diff --git a/packages/functions/src/capture/getAll.ts b/packages/functions/src/api/capture/getAll.ts similarity index 100% rename from packages/functions/src/capture/getAll.ts rename to packages/functions/src/api/capture/getAll.ts diff --git a/packages/functions/src/capture/post.ts b/packages/functions/src/api/capture/post.ts similarity index 100% rename from packages/functions/src/capture/post.ts rename to packages/functions/src/api/capture/post.ts diff --git a/packages/functions/src/recurringCapture/get.ts b/packages/functions/src/api/recurringCapture/get.ts similarity index 100% rename from packages/functions/src/recurringCapture/get.ts rename to packages/functions/src/api/recurringCapture/get.ts diff --git a/packages/functions/src/recurringCapture/post.ts b/packages/functions/src/api/recurringCapture/post.ts similarity index 98% rename from packages/functions/src/recurringCapture/post.ts rename to packages/functions/src/api/recurringCapture/post.ts index 96416fd..cb028e8 100644 --- a/packages/functions/src/recurringCapture/post.ts +++ b/packages/functions/src/api/recurringCapture/post.ts @@ -125,7 +125,9 @@ const postHandler = async (event: any) => { RetryPolicy: { MaximumRetryAttempts: 3, }, - Input: JSON.stringify(recursiveCapture), + Input: JSON.stringify({ + recursiveCaptureId: recursiveCapture.id, + }), }, FlexibleTimeWindow: { Mode: "OFF", diff --git a/packages/functions/src/recurringCapture/put.ts b/packages/functions/src/api/recurringCapture/put.ts similarity index 100% rename from packages/functions/src/recurringCapture/put.ts rename to packages/functions/src/api/recurringCapture/put.ts diff --git a/packages/functions/src/triggers/recurringHandler.ts b/packages/functions/src/triggers/recurringHandler.ts index 5b5f728..1f25fc4 100644 --- a/packages/functions/src/triggers/recurringHandler.ts +++ b/packages/functions/src/triggers/recurringHandler.ts @@ -1,24 +1,55 @@ import { Status } from "@website-capture/core/constants"; import { Capture } from "@website-capture/core/entity/Capture"; -import { ApiHandler } from "sst/node/api"; import { Config } from "sst/node/config"; -import { DeleteObjectCommand, S3Client } from "@aws-sdk/client-s3"; -import { Bucket } from "sst/node/bucket"; import middy from "@middy/core"; import { connectDatabase } from "@website-capture/core/middlewares"; +import { RecursiveCapture } from "@website-capture/core/entity/RecursiveCapture"; +import { SQSClient, SendMessageCommand } from "@aws-sdk/client-sqs"; +import { Queue } from "sst/node/queue"; type EventPayload = { - captureId: string; + recursiveCaptureId: string; }; -const s3Client = new S3Client({}); +const sqsClient = new SQSClient({}); // handler -const imageCleanerHandler = ApiHandler(async (event: any) => { - console.log(event); +const imageCleanerHandler = async (event: any) => { + const { recursiveCaptureId }: EventPayload = event; + + const recursiveCapture = await RecursiveCapture.findOneBy({ + id: recursiveCaptureId, + }); + + if (!recursiveCapture) { + throw new Error("Recursive capture not found"); + } + + // create Capture + const capture = new Capture(); + capture.website = recursiveCapture.website; + capture.width = recursiveCapture.width; + capture.height = recursiveCapture.height; + capture.format = recursiveCapture.format; + capture.owner = recursiveCapture.owner; + capture.recursiveCapture = recursiveCapture; + capture.status = Status.inProcess; + capture.recursiveCaptureId = recursiveCapture.id; + await capture.save(); + + // new sqs message + const queueURL = Queue.queue.queueUrl.toString(); + const command = new SendMessageCommand({ + QueueUrl: queueURL, + MessageBody: JSON.stringify({ + captureId: capture.id, + }), + DelaySeconds: 0, + }); + await sqsClient.send(command); return event; -}); +}; export const handler = middy(imageCleanerHandler).use([ connectDatabase(Config.POSTGRES_URL), diff --git a/stacks/MyStack.ts b/stacks/MyStack.ts index 644cdce..6752529 100644 --- a/stacks/MyStack.ts +++ b/stacks/MyStack.ts @@ -53,7 +53,7 @@ export function API({ stack, app }: StackContext) { stack, "CaptureCfnScheduleGroup", { - name: "CaptureCfnScheduleGroup", + name: `CaptureCfnScheduleGroup-${stack.stage}`, } ); @@ -63,11 +63,6 @@ export function API({ stack, app }: StackContext) { bind: [POSTGRES_URL, bucket], }); - const recurringCapture = new Function(stack, "recurringCapture", { - handler: "packages/functions/src/triggers/recurringHandler.handler", - bind: [POSTGRES_URL, bucket], - }); - // new queue const queue = new Queue(stack, "queue", { consumer: { @@ -107,6 +102,12 @@ export function API({ stack, app }: StackContext) { }, }); + // function to handle recurring capture + const recurringCapture = new Function(stack, "recurringCapture", { + handler: "packages/functions/src/triggers/recurringHandler.handler", + bind: [POSTGRES_URL, bucket, queue], + }); + const api = new Api(stack, "api", { authorizers: { jwt: { @@ -131,16 +132,16 @@ export function API({ stack, app }: StackContext) { authorizer: "jwt", }, routes: { - "POST /capture": "packages/functions/src/capture/post.handler", - "GET /capture": "packages/functions/src/capture/getAll.handler", - "GET /capture/{id}": "packages/functions/src/capture/get.handler", - "POST /test/{id}": "packages/functions/src/test.handler", + "POST /capture": "packages/functions/src/api/capture/post.handler", + "GET /capture": "packages/functions/src/api/capture/getAll.handler", + "GET /capture/{id}": "packages/functions/src/api/capture/get.handler", "POST /recurring-capture": - "packages/functions/src/recurringCapture/post.handler", + "packages/functions/src/api/recurringCapture/post.handler", "GET /recurring-capture/{id}": - "packages/functions/src/recurringCapture/get.handler", + "packages/functions/src/api/recurringCapture/get.handler", "PUT /recurring-capture/{id}": - "packages/functions/src/recurringCapture/put.handler", + "packages/functions/src/api/recurringCapture/put.handler", + "POST /test/{id}": "packages/functions/src/test.handler", }, });