migrate edit route
This commit is contained in:
parent
50c4e9ae2c
commit
154ca3aec0
@ -6,9 +6,9 @@
|
||||
import { ServerlessEventObject, ServerlessFunctionSignature } from "@twilio-labs/serverless-runtime-types/types";
|
||||
import { TwilioContext } from "../../../types/TwilioContext";
|
||||
import { shouldBlockRequest, UserAgentHeader } from "../../../utils/blockUserAgent";
|
||||
import { createDDBClient, ddbItemToJSON, getDoorConfigCommand, getDoorConfigUpdateCommand, putDoorUpdateConfigCommand, replaceDoorConfigWithUpdateItem } from "../../../utils/ddb";
|
||||
import { DoorConfig, EditDoorConfig } from "../../../types/DoorConfig";
|
||||
import { createDynaBridgeClient } from "../../../utils/ddb";
|
||||
import { sendMessageToUser } from "../../../utils/discord";
|
||||
import { createEditDoorConfig, EditDoorConfigReq, editDoorToDoorConfig, getDoorConfigID, getEditDoorConfigID } from "../../../schema/DoorConfig";
|
||||
|
||||
export interface EditRequest extends ServerlessEventObject<{}, UserAgentHeader> {
|
||||
door?: string;
|
||||
@ -28,19 +28,18 @@ export const handler: ServerlessFunctionSignature<TwilioContext, EditRequest> =
|
||||
let approvalId = event.approvalId;
|
||||
let newConfigString = event.newConfig;
|
||||
|
||||
const client = createDDBClient(context);
|
||||
const db = createDynaBridgeClient(context);
|
||||
|
||||
// approve path
|
||||
if (door && approvalId) {
|
||||
const newConfigDdb = await client.send(getDoorConfigUpdateCommand(door));
|
||||
const newConfig = ddbItemToJSON<EditDoorConfig>(newConfigDdb);
|
||||
const newConfig = await db.entities.editDoorConfig.findById(getEditDoorConfigID(door));
|
||||
|
||||
if (!newConfig || newConfig.approvalId !== approvalId) {
|
||||
response.setStatusCode(400);
|
||||
return callback(null, response);
|
||||
}
|
||||
|
||||
await client.send(replaceDoorConfigWithUpdateItem(newConfigDdb as any));
|
||||
db.entities.doorConfig.save(editDoorToDoorConfig(newConfig));
|
||||
|
||||
// send update to discord users
|
||||
const updateMessage = `Configuration change \`${approvalId}\` was approved @ Door "${door}"`;
|
||||
@ -64,10 +63,8 @@ export const handler: ServerlessFunctionSignature<TwilioContext, EditRequest> =
|
||||
return callback(null, response);
|
||||
}
|
||||
|
||||
const newConfig: EditDoorConfig = JSON.parse(newConfigString);
|
||||
|
||||
const configDdb = await client.send(getDoorConfigCommand(door));
|
||||
const config = ddbItemToJSON<DoorConfig>(configDdb);
|
||||
const newConfig: EditDoorConfigReq = JSON.parse(newConfigString);
|
||||
const config = await db.entities.doorConfig.findById(getDoorConfigID(door));
|
||||
|
||||
if (!config) {
|
||||
response.setStatusCode(404);
|
||||
@ -79,15 +76,14 @@ export const handler: ServerlessFunctionSignature<TwilioContext, EditRequest> =
|
||||
newConfig.pin = config.pin;
|
||||
}
|
||||
|
||||
const input = putDoorUpdateConfigCommand(door, newConfig);
|
||||
|
||||
const update = await client.send(input);
|
||||
const editDoorConfig = createEditDoorConfig(door, newConfig);
|
||||
await db.entities.editDoorConfig.save(editDoorConfig);
|
||||
|
||||
// newConfig.discordUsers = undefined;
|
||||
// newConfig.fallbackNumbers = undefined;
|
||||
// newConfig.status = undefined;
|
||||
|
||||
const approvalUrl = `https://doorman.chromart.cc/api/door/edit?door=${door}&approvalId=${input?.input?.Item?.approvalId.S as string}`;
|
||||
const approvalUrl = `https://doorman.chromart.cc/api/door/edit?door=${door}&approvalId=${editDoorConfig.approvalId as string}`;
|
||||
console.log(approvalUrl);
|
||||
|
||||
// send update to discord users
|
||||
@ -106,8 +102,11 @@ export const handler: ServerlessFunctionSignature<TwilioContext, EditRequest> =
|
||||
response
|
||||
.setStatusCode(200)
|
||||
.appendHeader('Content-Type', 'application/json')
|
||||
.setBody({ msg: update });
|
||||
.setBody({ msg: "Created Configuration change" });
|
||||
|
||||
// destroy the internal client after
|
||||
// @ts-ignore
|
||||
db.ddbClient.destroy();
|
||||
|
||||
await client.destroy();
|
||||
return callback(null, response);
|
||||
};
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { z } from "zod";
|
||||
import { DynaBridgeEntity } from 'dynabridge';
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
export const DOOR_CONFIG_SK = "config";
|
||||
export const EDIT_DOOR_CONFIG_SK = "config-update";
|
||||
@ -36,6 +37,8 @@ export const getEditDoorConfigID = (doorName: string): string[] => {
|
||||
export type DoorConfig = z.infer<typeof DoorConfigSchema>;
|
||||
export type EditDoorConfig = z.infer<typeof EditDoorConfigSchema>;
|
||||
|
||||
export type EditDoorConfigReq = Omit<EditDoorConfig, "PK" | "SK" | "approvalId">;
|
||||
|
||||
export const DoorConfigEntity: DynaBridgeEntity<DoorConfig> = {
|
||||
tableName: "doorman",
|
||||
id: ["PK", "SK"],
|
||||
@ -45,3 +48,23 @@ export const EditDoorConfigEntity: DynaBridgeEntity<EditDoorConfig> = {
|
||||
tableName: "doorman",
|
||||
id: ["PK", "SK"],
|
||||
};
|
||||
|
||||
export const editDoorToDoorConfig = (updateDoor: EditDoorConfig): DoorConfig => {
|
||||
const newItem: any = {
|
||||
...updateDoor,
|
||||
SK: DOOR_CONFIG_SK,
|
||||
};
|
||||
|
||||
delete newItem.approvalId;
|
||||
|
||||
return newItem;
|
||||
}
|
||||
|
||||
export const createEditDoorConfig = (door: string, newConfig: EditDoorConfigReq): EditDoorConfig => {
|
||||
return {
|
||||
...newConfig,
|
||||
PK: "door-" + door,
|
||||
SK: EDIT_DOOR_CONFIG_SK,
|
||||
approvalId: randomUUID().toString(),
|
||||
};
|
||||
}
|
||||
|
||||
@ -15,7 +15,7 @@
|
||||
"@twilio/runtime-handler": "1.3.0",
|
||||
"node-fetch": "^2.7.0",
|
||||
"prom-client": "^15.1.3",
|
||||
"prometheus-remote-write": "^0.5.0",
|
||||
"prometheus-remote-write": "^0.5.1",
|
||||
"promise.timeout": "^1.2.0",
|
||||
"twilio": "^3.84.1",
|
||||
"winston": "^3.17.0",
|
||||
@ -23,7 +23,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest",
|
||||
"concurrently": "^9.1.0",
|
||||
"concurrently": "^9.1.2",
|
||||
"twilio-run": "^3.5.4"
|
||||
},
|
||||
"engines": {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user