Martin Dimitrov c1f068f5aa
All checks were successful
Build and push image for doorman / docker (push) Successful in 1m0s
change to ddb instead of redis
2024-07-06 13:40:06 -07:00

66 lines
1.3 KiB
JavaScript

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),
}
}
});
};