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