migrate status route
This commit is contained in:
parent
154ca3aec0
commit
ca2cd5286f
@ -5,8 +5,9 @@
|
||||
import { ServerlessFunctionSignature, ServerlessEventObject } from "@twilio-labs/serverless-runtime-types/types";
|
||||
import { TwilioContext } from "../../../types/TwilioContext";
|
||||
import { shouldBlockRequest } from "../../../utils/blockUserAgent";
|
||||
import { clearLockStatusCommand, createDDBClient, getLockStatusCommand, isLockOpen } from "../../../utils/ddb";
|
||||
import { createDynaBridgeClient } from "../../../utils/ddb";
|
||||
import { DoorStatus } from "../../../types/DoorStatus";
|
||||
import { getLockStatusID, isLockOpen } from "../../../schema/LockStatus";
|
||||
|
||||
export interface StatusRequest extends ServerlessEventObject {
|
||||
door: string;
|
||||
@ -32,14 +33,13 @@ export const handler: ServerlessFunctionSignature<TwilioContext, StatusRequest>
|
||||
return callback(null, response);
|
||||
}
|
||||
|
||||
const client = createDDBClient(context);
|
||||
const db = createDynaBridgeClient(context);
|
||||
const lock = await db.entities.lockStatus.findById(getLockStatusID(door));
|
||||
|
||||
await client.send(getLockStatusCommand(door))
|
||||
.then(async (lock) => {
|
||||
const isOpen = isLockOpen(lock);
|
||||
|
||||
if (isOpen) {
|
||||
const fingerprint = JSON.parse(lock?.Item?.fingerprint.S as string);
|
||||
if (isOpen && lock) {
|
||||
const fingerprint = JSON.parse(lock.fingerprint);
|
||||
|
||||
response
|
||||
.setStatusCode(200)
|
||||
@ -49,25 +49,18 @@ export const handler: ServerlessFunctionSignature<TwilioContext, StatusRequest>
|
||||
fingerprint,
|
||||
});
|
||||
|
||||
await client.send(clearLockStatusCommand(lock));
|
||||
return;
|
||||
}
|
||||
|
||||
await db.entities.lockStatus.deleteById(getLockStatusID(door));
|
||||
} else {
|
||||
response
|
||||
.setStatusCode(200)
|
||||
.appendHeader('Content-Type', 'application/json')
|
||||
.setBody({
|
||||
status: DoorStatus.CLOSED,
|
||||
});
|
||||
}
|
||||
|
||||
}).catch((e) => {
|
||||
console.log(e);
|
||||
response
|
||||
.setStatusCode(500)
|
||||
.appendHeader('Content-Type', 'application/json')
|
||||
.setBody({ err: e });
|
||||
});
|
||||
|
||||
await client.destroy();
|
||||
// destroy the internal client after
|
||||
// @ts-ignore
|
||||
db.ddbClient.destroy();
|
||||
return callback(null, response);
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user