component-dbs-core
Version:
DTO objects shared among device backend
138 lines • 7.25 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.TransactionService = void 0;
const common_1 = require("@nestjs/common");
const graphql_tag_1 = __importDefault(require("graphql-tag"));
const transactionDb_1 = require("./transactionDb");
const transactionUtils_1 = require("./transactionUtils");
const devices_1 = require("../devices");
const common_services_1 = require("../common_services");
const common_interfaces_1 = require("../common_interfaces");
let TransactionService = class TransactionService extends common_interfaces_1.UpdateEventPublisher {
constructor(dynamodbService, envService, deviceVerify, deviceService, appsyncClient, lambdaService) {
super(appsyncClient, (deviceUuid) => this.deviceService.getDeviceStatus(deviceUuid));
this.dynamodbService = dynamodbService;
this.envService = envService;
this.deviceVerify = deviceVerify;
this.deviceService = deviceService;
this.appsyncClient = appsyncClient;
this.lambdaService = lambdaService;
this.requestTransaction = (accessToken, event) => __awaiter(this, void 0, void 0, function* () {
console.log(event);
const cqrsCommandHandler = this.envService.cqrsCommandHandler;
console.log('send transaction request to ', cqrsCommandHandler);
try {
const userIdentityId = yield this.deviceService.getEntityUuidByAccessToken(accessToken);
const transactionRequest = {
transactionId: event.id,
catid: event.catid,
caid: event.caid,
iso8583: event.iso8583,
userIdentityId,
};
const output = yield this.lambdaService.lambda
.invoke({
FunctionName: cqrsCommandHandler,
Payload: JSON.stringify(transactionRequest),
})
.promise();
console.log('invoke command handler lambda response:', output);
return { status: true };
}
catch (err) {
console.error(err);
}
return { status: false };
});
this.getTransaction = (transactionUuid) => __awaiter(this, void 0, void 0, function* () {
const output = yield this.transactionDb.getTransaction(transactionUuid);
if (output.Items && output.Items.length > 0) {
return transactionUtils_1.convertDbItemToTransaction(output.Items[0]);
}
throw new Error(`Transaction ${transactionUuid} not found.`);
});
this.getTransactions = (event, accessToken) => __awaiter(this, void 0, void 0, function* () {
if (event.deviceUuid &&
!(yield this.deviceVerify.isDeviceTokenValid(event.deviceUuid, accessToken))) {
console.log(`Access token ${accessToken} doesnt mathch device uuid: ${event.deviceUuid}`);
throw new Error('Invalid access token.');
}
const entityUuid = yield this.deviceService.getEntityUuidByAccessToken(accessToken);
console.log('get entity uuid:', entityUuid);
const output = yield this.transactionDb.getTransactions(event, entityUuid);
if (output && output.Items && output.Items.length > 0) {
const transactions = output.Items.map((item) => transactionUtils_1.convertDbItemToTransaction(item));
const nextToken = output.LastEvaluatedKey
? output.LastEvaluatedKey
: undefined;
const response = nextToken
? { transactions, nextToken }
: { transactions };
console.log('response:', response);
return response;
}
return { transactions: [] };
});
this.onTransactionStream = (event) => __awaiter(this, void 0, void 0, function* () {
console.info('transaction update event:', JSON.stringify(event));
yield this.publishUpdateEventToSubscribers(event, transactionUtils_1.convertDbStreamToTransaction);
});
this.onTransactionUpdate = (accessToken) => __awaiter(this, void 0, void 0, function* () {
yield this.deviceVerify.checkAccessToken(accessToken);
});
this.transactionDb = new transactionDb_1.TransactionDb(this.envService.deviceTableName, this.dynamodbService, envService.transactionGsi, envService.accessTokenGsi, envService.entityUuidGsi, envService.siteUuidGsi);
}
subscribeTransactionResponse(event) {
// TODO - implement me - subscribe transaction response handler
console.log(event);
return { id: event.args.id };
}
prepareUpdateEventRequest(item, keys) {
return {
mutation: graphql_tag_1.default `
mutation publishTransactionUpdateEvent(
$item: TransactionInput!
) {
publishTransactionUpdateEvent(transaction: $item) {
${keys}
}
}
`,
variables: {
item,
},
};
}
};
TransactionService = __decorate([
common_1.Injectable(),
__metadata("design:paramtypes", [common_services_1.DynamodbService,
common_services_1.EnvironmentService,
devices_1.DeviceVerification,
devices_1.DeviceService,
common_services_1.AppSyncClient,
common_services_1.LambdaService])
], TransactionService);
exports.TransactionService = TransactionService;
//# sourceMappingURL=transactionService.js.map