UNPKG

phx-node

Version:

PHX NODE

93 lines 4.84 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 PHXPushNotificationService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.PHXPushNotificationService = exports.NotificationType = exports.EmailType = 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"); var EmailType; (function (EmailType) { EmailType["TRANSACTION_BASIC"] = "transaction-basic"; EmailType["HRM_REMIND"] = "hrm-remind"; EmailType["ORDER_SUCCESS"] = "order-success"; EmailType["FREESTYLE_CAMPAIGN"] = "freestyle-campaign"; })(EmailType || (exports.EmailType = EmailType = {})); var NotificationType; (function (NotificationType) { NotificationType["SYSTEM"] = "SYSTEM"; NotificationType["TO_ME"] = "TO_ME"; })(NotificationType || (exports.NotificationType = NotificationType = {})); let PHXPushNotificationService = PHXPushNotificationService_1 = class PHXPushNotificationService { constructor(httpService, configService) { this.httpService = httpService; this.configService = configService; this.logger = new common_1.Logger(PHXPushNotificationService_1.name); } pushNoti(payload) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; 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`, payload, { 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: ${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"); } }); } }; exports.PHXPushNotificationService = PHXPushNotificationService; exports.PHXPushNotificationService = PHXPushNotificationService = PHXPushNotificationService_1 = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [axios_1.HttpService, config_1.ConfigService]) ], PHXPushNotificationService); //# sourceMappingURL=push-notification.service.js.map