178 lines
3.3 KiB
JavaScript
178 lines
3.3 KiB
JavaScript
const { randomUUID } = require("crypto");
|
|
const { DynamoDBClient, GetItemCommand, DeleteItemCommand, PutItemCommand, UpdateItemCommand } = 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.getDoorAliasCommand = (buzzerNumber) => {
|
|
return new GetItemCommand({
|
|
TableName: "doorman",
|
|
Key: {
|
|
"PK": {
|
|
S: buzzerNumber,
|
|
},
|
|
"SK": {
|
|
S: "alias",
|
|
}
|
|
},
|
|
});
|
|
};
|
|
|
|
exports.getDoorConfigCommand = (door) => {
|
|
return new GetItemCommand({
|
|
TableName: "doorman",
|
|
Key: {
|
|
"PK": {
|
|
S: `door-${door}`,
|
|
},
|
|
"SK": {
|
|
S: "config",
|
|
},
|
|
},
|
|
});
|
|
};
|
|
|
|
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),
|
|
}
|
|
}
|
|
});
|
|
};
|
|
|
|
exports.putDoorUpdateConfigCommand = (door, config) => {
|
|
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(),
|
|
}
|
|
}
|
|
});
|
|
};
|
|
|
|
exports.getDoorConfigCommand = (door) => {
|
|
return new GetItemCommand({
|
|
TableName: "doorman",
|
|
Key: {
|
|
"PK": {
|
|
S: `door-${door}`,
|
|
},
|
|
"SK": {
|
|
S: "config-update",
|
|
},
|
|
},
|
|
});
|
|
};
|
|
|
|
exports.getDoorConfigUpdateCommand = (door) => {
|
|
return new GetItemCommand({
|
|
TableName: "doorman",
|
|
Key: {
|
|
"PK": {
|
|
S: `door-${door}`,
|
|
},
|
|
"SK": {
|
|
S: "config-update",
|
|
},
|
|
},
|
|
});
|
|
};
|
|
|
|
exports.replaceDoorConfigWithUpdateItem = (newConfigItem) => {
|
|
const newItem = {
|
|
...newConfigItem.Item,
|
|
SK: { S: "config" },
|
|
};
|
|
|
|
delete newItem.approvalId;
|
|
|
|
return new PutItemCommand({
|
|
TableName: "doorman",
|
|
Item: newItem,
|
|
});
|
|
};
|