Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | 10x 43x 43x 43x 43x 43x 43x 35x 35x 35x 43x 31x 31x 31x 31x 43x 108x 108x 43x 6x 6x 43x 11x 11x 11x 2x 2x 11x 9x 9x 11x 11x 43x 97x 97x 97x 97x 97x 97x 97x 182x 182x 158x 97x 260x 101x 57x 57x 158x 158x 62x 97x 1x 97x 97x 57x 97x 43x 4x 4x 4x 4x 43x 66x 43x 8x 8x | import { DocumentClient } from 'aws-sdk/clients/dynamodb';
import { DeviceType } from './deviceTypes.d';
import { JsonObject } from '../types.d';
export class DeviceDb {
deviceUuidKeyCondition = 'id = :id and #type = :type';
constructor(
private readonly deviceTableName: string,
private readonly modelSerialGsi: string,
private readonly dynamodbClient: DocumentClient,
private readonly accessTokenGsi: string,
) {}
getDeviceByModelSerial = async (
model: string,
serial: string,
): Promise<DocumentClient.QueryOutput> => {
/* eslint-disable */
const params = {
TableName: this.deviceTableName,
IndexName: this.modelSerialGsi,
ExpressionAttributeValues: {
':modelSerial': this.createModelSerialKey(model, serial),
},
KeyConditionExpression: 'modelSerial = :modelSerial',
};
return this.dynamodbClient.query(params).promise();
};
saveDeviceUuid = async (
model: string,
serial: string,
id: string,
): Promise<DocumentClient.PutItemOutput> => {
const modelSerial = this.createModelSerialKey(model, serial);
const params: DocumentClient.PutItemInput = {
TableName: this.deviceTableName,
Item: {
id,
modelSerial,
model,
serial,
type: DeviceType.core,
},
};
return this.dynamodbClient.put(params).promise();
};
getDeviceByUuid = (id: string): Promise<DocumentClient.QueryOutput> => {
const params: DocumentClient.QueryInput = {
TableName: this.deviceTableName,
ExpressionAttributeValues: {
':id': id,
':type': DeviceType.core,
},
KeyConditionExpression: this.deviceUuidKeyCondition,
ExpressionAttributeNames: { '#type': 'type' },
};
return this.dynamodbClient.query(params).promise();
};
getDeviceRecordByType = (id: string, type: DeviceType): Promise<DocumentClient.QueryOutput> => {
const params: DocumentClient.QueryInput = {
TableName: this.deviceTableName,
ExpressionAttributeValues: {
":id": id,
":type": type,
},
KeyConditionExpression: this.deviceUuidKeyCondition,
ExpressionAttributeNames: { "#type": "type" },
};
return this.dynamodbClient.query(params).promise();
};
findDeviceByTokens = (
deviceUuid: string,
accessToken: string,
refreshToken?: string,
idToken?: string,
): Promise<DocumentClient.QueryOutput> => {
const attrValues: { [key: string]: string } = {
':id': deviceUuid,
':type': DeviceType.core,
':accessToken': accessToken,
};
let filterExpression = 'accessToken = :accessToken';
if (idToken) {
filterExpression += ' and idToken = :idToken';
attrValues[':idToken'] = idToken;
}
if (refreshToken) {
filterExpression += ' and refreshToken = :refreshToken';
attrValues[':refreshToken'] = refreshToken;
}
const params: DocumentClient.QueryInput = {
TableName: this.deviceTableName,
ExpressionAttributeValues: attrValues,
KeyConditionExpression: this.deviceUuidKeyCondition,
FilterExpression: filterExpression,
ExpressionAttributeNames: { '#type': 'type' },
};
return this.dynamodbClient.query(params).promise();
};
updateDeviceRecord = async (
deviceUuid: string,
type: DeviceType,
values: { [key: string]: any },
) => {
let updateExpression = "";
const reservedWords = ['status', 'name']
const attrValues: JsonObject<string> = {};
const attrNames: JsonObject<string> = {};
const keys = Object.keys(values);
keys.forEach((key: string, i: number) => {
const v = values[key];
if (v) {
if (i === 0) {
updateExpression = "set";
}
if(!reservedWords.some( w => w === key)){
updateExpression += ` ${key} = :${key}`;
}
else {
updateExpression += ` #${key} = :${key}`
attrNames[`#${key}`] = key;
}
attrValues[`:${key}`] = v;
if (i < keys.length - 1) {
updateExpression += ',';
}
}
});
if (updateExpression.endsWith(',')) {
updateExpression = updateExpression.slice(0, -1);
}
const params: DocumentClient.UpdateItemInput = {
TableName: this.deviceTableName,
Key: {
id: deviceUuid,
type,
},
UpdateExpression: updateExpression,
ExpressionAttributeValues: attrValues
};
if (Object.keys(attrNames).length > 0) {
params.ExpressionAttributeNames = attrNames;
}
return this.dynamodbClient.update(params).promise();
};
findDeviceByPin = (
pin: string,
deviceUuid: string,
): Promise<DocumentClient.QueryOutput> => {
const attrValues = {
':id': deviceUuid,
':type': DeviceType.settings,
':pin': pin,
};
const filterExpression = 'pin = :pin';
const params: DocumentClient.QueryInput = {
TableName: this.deviceTableName,
ExpressionAttributeValues: attrValues,
KeyConditionExpression: this.deviceUuidKeyCondition,
FilterExpression: filterExpression,
ExpressionAttributeNames: { '#type': 'type' },
};
return this.dynamodbClient.query(params).promise();
};
createModelSerialKey = (model: string, serial: string): string => {
return `${model}.${serial}`;
};
getDeviceByAccessToken = (
token: string,
): Promise<DocumentClient.QueryOutput> => {
// TODO: we need some way to verify entity uuid based on access token
const params: DocumentClient.QueryInput = {
TableName: this.deviceTableName,
IndexName: this.accessTokenGsi,
ExpressionAttributeValues: {
':accessToken': token,
},
KeyConditionExpression: 'accessToken = :accessToken',
};
return this.dynamodbClient.query(params).promise();
};
}
|