const { DynamoDBClient, GetItemCommand, DeleteItemCommand, PutItemCommand } = require("@aws-sdk/client-dynamodb"); exports.createDDBClient = (context) => { return new DynamoDBClient({ region: "us-east-1" , credentials: { accessKeyId: context.AWS_ACCESS_KEY, secretAccessKey: context.AWS_SECRET_ACCESS_KEY, }, }); }; exports.getLockStatusCommand = (door) => { return new GetItemCommand({ TableName: "doorman", Key: { "PK": { S: door, }, "SK": { S: "lock", } }, }); }; exports.isLockOpen = (lock) => { // ttl is a UTC ms time for how long it is unlocked const ttl = lock.Item?.TTL?.N || 0; return ttl > Date.now(); }; exports.clearLockStatusCommand = (lock) => { return new DeleteItemCommand({ TableName: "doorman", Key: { "PK": { S: lock.Item.PK.S, }, "SK": { S: "lock", } } }); }; exports.setLockStatusCommand = (door, timeoutSeconds, fingerprintObj) => { return new PutItemCommand({ TableName: "doorman", Item: { "PK": { S: door, }, "SK": { S: "lock", }, "TTL": { N: `${Date.now() + timeoutSeconds * 1000}`, }, "fingerprint": { S: JSON.stringify(fingerprintObj), } } }); };