Skip to content

Commit

Permalink
Refactor API
Browse files Browse the repository at this point in the history
  • Loading branch information
tranlehaiquan committed Dec 17, 2023
1 parent 8dcead0 commit bb5a783
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 23 deletions.
9 changes: 9 additions & 0 deletions packages/core/src/entity/Capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
7 changes: 6 additions & 1 deletion packages/core/src/entity/RecursiveCapture.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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[];
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
47 changes: 39 additions & 8 deletions packages/functions/src/triggers/recurringHandler.ts
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
27 changes: 14 additions & 13 deletions stacks/MyStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function API({ stack, app }: StackContext) {
stack,
"CaptureCfnScheduleGroup",
{
name: "CaptureCfnScheduleGroup",
name: `CaptureCfnScheduleGroup-${stack.stage}`,
}
);

Expand All @@ -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: {
Expand Down Expand Up @@ -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: {
Expand All @@ -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",
},
});

Expand Down

0 comments on commit bb5a783

Please sign in to comment.