UNPKG

phx-node

Version:

PHX NODE

127 lines 6.57 kB
"use strict"; 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 __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var PHXPushNotificationServiceV2_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.PHXPushNotificationServiceV2 = void 0; const axios_1 = require("@nestjs/axios"); const common_1 = require("@nestjs/common"); const config_1 = require("@nestjs/config"); const rxjs_1 = require("rxjs"); let PHXPushNotificationServiceV2 = PHXPushNotificationServiceV2_1 = class PHXPushNotificationServiceV2 { constructor(httpService, configService) { this.httpService = httpService; this.configService = configService; this.logger = new common_1.Logger(PHXPushNotificationServiceV2_1.name); } pushNoti(payload) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const { hostname } = payload, dto = __rest(payload, ["hostname"]); const producerApi = this.configService.get("PRODUCER_API"); if (!producerApi) { const errorMessage = `${hostname}: PRODUCER_API is not configured`; this.logger.debug(errorMessage); throw new Error(errorMessage); } try { const { data } = yield (0, rxjs_1.lastValueFrom)(this.httpService.post(`${producerApi}/emit/inapp-noti/v2`, dto, { headers: { hostname }, })); return data; } catch (error) { let errorMessage = ""; if ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) { const message = error.response.data.message; errorMessage = Array.isArray(message) ? message.join(", ") : message; } const errorResponse = `Error pushing in-app notification v2: ${errorMessage}`; this.logger.error(errorResponse); throw new Error(errorResponse); } }); } complete(_a) { return __awaiter(this, arguments, void 0, function* ({ payload }) { var _b, _c; const { hostname } = payload; const producerApi = this.configService.get("PRODUCER_API"); if (!producerApi) { const errorMessage = `${hostname}: PRODUCER_API is not configured`; this.logger.debug(errorMessage); throw new Error(errorMessage); } try { const { data } = yield (0, rxjs_1.lastValueFrom)(this.httpService.post(`${producerApi}/emit/inapp-noti/complete-require-action`, payload, { headers: { hostname } })); return data; } catch (error) { let errorMessage = ""; if ((_c = (_b = error.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.message) { const message = error.response.data.message; errorMessage = Array.isArray(message) ? message.join(", ") : message; } const errorResponse = `Error complete require action: ${errorMessage}`; this.logger.error(errorResponse); throw new Error(errorResponse); } }); } send(payload) { return __awaiter(this, void 0, void 0, function* () { try { yield this.pushNoti(payload); return { status: "success", message: "Notification pushed successfully" }; } catch (error) { throw new common_1.BadRequestException((error === null || error === void 0 ? void 0 : error.message) || "Failed to process notification"); } }); } completeRequireAction(payload) { return __awaiter(this, void 0, void 0, function* () { try { yield this.complete({ payload }); return { status: "success", message: "Action completed successfully" }; } catch (error) { throw new common_1.BadRequestException((error === null || error === void 0 ? void 0 : error.message) || "Failed to complete action"); } }); } }; exports.PHXPushNotificationServiceV2 = PHXPushNotificationServiceV2; exports.PHXPushNotificationServiceV2 = PHXPushNotificationServiceV2 = PHXPushNotificationServiceV2_1 = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [axios_1.HttpService, config_1.ConfigService]) ], PHXPushNotificationServiceV2); //# sourceMappingURL=push-notification-v2.service.js.map