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