component-dbs-core
Version:
DTO objects shared among device backend
95 lines • 5.07 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DepositService = void 0;
const common_1 = require("@nestjs/common");
const graphql_tag_1 = __importDefault(require("graphql-tag"));
const depositDb_1 = require("./depositDb");
const devices_1 = require("../devices");
const depositUtils_1 = require("./depositUtils");
const common_services_1 = require("../common_services");
const common_interfaces_1 = require("../common_interfaces");
let DepositService = class DepositService extends common_interfaces_1.UpdateEventPublisher {
constructor(dbService, envService, deviceService, deviceVerify, appsyncClient) {
super(appsyncClient, (deviceUuid) => this.deviceService.getDeviceStatus(deviceUuid));
this.dbService = dbService;
this.envService = envService;
this.deviceService = deviceService;
this.deviceVerify = deviceVerify;
this.appsyncClient = appsyncClient;
this.getDeposit = (depositUuid) => __awaiter(this, void 0, void 0, function* () {
const output = yield this.depositDb.getDeposit(depositUuid);
if (output.Items && output.Items.length > 0) {
return depositUtils_1.convertDbItemToDeposit(output.Items[0]);
}
throw new Error(`Deposit ${depositUuid} not found.`);
});
this.getDeposits = (event, accessToken) => __awaiter(this, void 0, void 0, function* () {
const entityUuid = yield this.deviceService.getEntityUuidByAccessToken(accessToken);
console.log('get entity uuid:', entityUuid);
const output = yield this.depositDb.getDeposits(event, entityUuid);
if (output && output.Items && output.Items.length > 0) {
const deposits = output.Items.map((item) => depositUtils_1.convertDbItemToDeposit(item));
const nextToken = output.LastEvaluatedKey
? output.LastEvaluatedKey
: undefined;
return nextToken ? { deposits, nextToken } : { deposits };
}
return { deposits: [] };
});
this.onDepositStream = (event) => __awaiter(this, void 0, void 0, function* () {
console.info('deposit stream event:', JSON.stringify(event));
yield this.publishUpdateEventToSubscribers(event, depositUtils_1.convertDbStreamToDeposit);
});
this.onDepositUpdate = (accessToken) => __awaiter(this, void 0, void 0, function* () {
yield this.deviceVerify.checkAccessToken(accessToken);
});
this.depositDb = new depositDb_1.DepositDb(this.envService.deviceTableName, this.dbService, envService.depositGsi, envService.entityUuidGsi);
}
prepareUpdateEventRequest(item, keys) {
return {
mutation: graphql_tag_1.default `
mutation publishDepositUpdateEvent(
$item: DepositInput!
) {
publishDepositUpdateEvent(deposit: $item) {
${keys}
}
}
`,
variables: {
item,
},
};
}
};
DepositService = __decorate([
common_1.Injectable(),
__metadata("design:paramtypes", [common_services_1.DynamodbService,
common_services_1.EnvironmentService,
devices_1.DeviceService,
devices_1.DeviceVerification,
common_services_1.AppSyncClient])
], DepositService);
exports.DepositService = DepositService;
//# sourceMappingURL=depositService.js.map