From 2f45005a8a37eefad643b7f6fd2eaf92c14125a3 Mon Sep 17 00:00:00 2001 From: Martin Dimitrov Date: Sat, 31 May 2025 21:50:31 -0700 Subject: [PATCH] cleanup old code --- packages/doorman-api/src/types/DoorConfig.ts | 14 -- packages/doorman-api/src/types/Lock.ts | 3 - packages/doorman-api/src/utils/ddb.ts | 180 ------------------- 3 files changed, 197 deletions(-) delete mode 100644 packages/doorman-api/src/types/DoorConfig.ts delete mode 100644 packages/doorman-api/src/types/Lock.ts diff --git a/packages/doorman-api/src/types/DoorConfig.ts b/packages/doorman-api/src/types/DoorConfig.ts deleted file mode 100644 index 297a99e..0000000 --- a/packages/doorman-api/src/types/DoorConfig.ts +++ /dev/null @@ -1,14 +0,0 @@ -export interface DoorConfig { - buzzer: string; - buzzerCode: string; - discordUsers: string[]; - fallbackNumbers: string[]; - pin: string; - pressKey: string; - timeout: number; - greeting: string; -} - -export interface EditDoorConfig extends DoorConfig { - approvalId: string; -} diff --git a/packages/doorman-api/src/types/Lock.ts b/packages/doorman-api/src/types/Lock.ts deleted file mode 100644 index d0608f2..0000000 --- a/packages/doorman-api/src/types/Lock.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface Lock { - fingerprint: any; -} diff --git a/packages/doorman-api/src/utils/ddb.ts b/packages/doorman-api/src/utils/ddb.ts index b72345d..ea08565 100644 --- a/packages/doorman-api/src/utils/ddb.ts +++ b/packages/doorman-api/src/utils/ddb.ts @@ -1,22 +1,9 @@ -import { randomUUID } from "crypto"; -import { DynamoDBClient, GetItemCommand, DeleteItemCommand, PutItemCommand, UpdateItemCommand, GetItemOutput } from "@aws-sdk/client-dynamodb"; import { TwilioContext } from "../types/TwilioContext"; -import { DoorConfig } from "../types/DoorConfig"; import { DynaBridge } from "dynabridge"; import { DoorConfigEntity, EditDoorConfigEntity } from "../schema/DoorConfig"; import { DoorAliasEntity } from "../schema/DoorAlias"; import { LockStatusEntity } from "../schema/LockStatus"; -export const createDDBClient = (context: TwilioContext) => { - return new DynamoDBClient({ - region: "us-east-1" , - credentials: { - accessKeyId: context.AWS_ACCESS_KEY, - secretAccessKey: context.AWS_SECRET_ACCESS_KEY, - }, - }); -}; - export const createDynaBridgeClient = (context: TwilioContext) => { // register all entities here return new DynaBridge({ @@ -46,170 +33,3 @@ export const createDynaBridgeClient = (context: TwilioContext) => { }, }); }; - -export const getLockStatusCommand = (door: string) => { - return new GetItemCommand({ - TableName: "doorman", - Key: { - "PK": { - S: door, - }, - "SK": { - S: "lock", - } - }, - }); -}; - -export const getDoorAliasCommand = (buzzerNumber: string) => { - return new GetItemCommand({ - TableName: "doorman", - Key: { - "PK": { - S: buzzerNumber, - }, - "SK": { - S: "alias", - } - }, - }); -}; - -export const getDoorConfigCommand = (door: string) => { - return new GetItemCommand({ - TableName: "doorman", - Key: { - "PK": { - S: `door-${door}`, - }, - "SK": { - S: "config", - }, - }, - }); -}; - -export const isLockOpen = (lock: GetItemOutput) => { - // ttl is a UTC ms time for how long it is unlocked - const ttl = lock.Item?.TTL?.N || 0; - - return parseInt("" + ttl) > Date.now(); -}; - -export const clearLockStatusCommand = (lock: GetItemOutput) => { - return new DeleteItemCommand({ - TableName: "doorman", - Key: { - "PK": { - S: lock?.Item?.PK.S as string, - }, - "SK": { - S: "lock", - } - } - }); -}; - -export const setLockStatusCommand = (door: string, timeoutSeconds: number, fingerprintObj: any) => { - return new PutItemCommand({ - TableName: "doorman", - Item: { - "PK": { - S: door, - }, - "SK": { - S: "lock", - }, - "TTL": { - N: `${Date.now() + timeoutSeconds * 1000}`, - }, - "fingerprint": { - S: JSON.stringify(fingerprintObj), - } - } - }); -}; - -export const putDoorUpdateConfigCommand = (door: string, config: DoorConfig) => { - return new PutItemCommand({ - TableName: "doorman", - Item: { - "PK": { - S: "door-" + door, - }, - "SK": { - S: "config-update", - }, - "buzzer": { - S: config.buzzer, - }, - "buzzerCode": { - S: config.buzzerCode, - }, - "discordUsers": { - SS: config.discordUsers, - }, - "fallbackNumbers": { - SS: config.fallbackNumbers, - }, - "pin": { - S: config.pin, - }, - "pressKey": { - S: config.pressKey, - }, - "timeout": { - N: `${config.timeout}`, - }, - "greeting": { - S: config.greeting, - }, - "approvalId": { - S: randomUUID().toString(), - } - } - }); -}; - -export function ddbItemToJSON>(ddbItem: GetItemOutput): T { - let obj: any = {}; - - if (!ddbItem.Item) { - return obj; - } - - const Item = ddbItem.Item; - - Object.keys(Item).forEach(key => { - obj[key] = Object.values(Item[key])[0]; - }); - return obj; -}; - -export const getDoorConfigUpdateCommand = (door: string) => { - return new GetItemCommand({ - TableName: "doorman", - Key: { - "PK": { - S: `door-${door}`, - }, - "SK": { - S: "config-update", - }, - }, - }); -}; - -export const replaceDoorConfigWithUpdateItem = (newConfigItem: GetItemOutput & { Item: { approvalId?: { S: string } }}) => { - const newItem = { - ...newConfigItem.Item, - SK: { S: "config" }, - }; - - delete newItem.approvalId; - - return new PutItemCommand({ - TableName: "doorman", - Item: newItem, - }); -};