phx-react
Version:
PHX REACT
56 lines • 2.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationType = exports.EmailType = void 0;
exports.PHXPushNotificationService = PHXPushNotificationService;
const tslib_1 = require("tslib");
const axios_1 = tslib_1.__importDefault(require("axios"));
const read_env_config_1 = require("../../../read-env-config");
/* ====== Enums ====== */
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 = {}));
/* ====== Main Function ====== */
async function PHXPushNotificationService(payload) {
var _a, _b;
const producerApi = (0, read_env_config_1.getEnv)('NEXT_PUBLIC_PRODUCER_API', process.env.NEXT_PUBLIC_PRODUCER_API);
const { hostname } = payload;
if (!producerApi) {
const errorMessage = `${hostname}: PRODUCER_API is not configured`;
console.debug(errorMessage);
throw new Error(errorMessage);
}
try {
const { data } = await axios_1.default.post(`${producerApi}/emit/inapp-noti`, payload, {
headers: {
hostname,
},
});
return (data !== null && data !== void 0 ? data : {
status: 'success',
message: 'Notification pushed successfully',
});
}
catch (error) {
let errorMessage = '';
if ((_b = (_a = error === null || error === void 0 ? void 0 : 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;
}
else if (error === null || error === void 0 ? void 0 : error.message) {
errorMessage = error.message;
}
const finalMessage = `Error pushing in-app notification: ${errorMessage}`;
console.error(finalMessage);
throw new Error(finalMessage || 'Failed to process notification');
}
}
//# sourceMappingURL=pushNotificationService.js.map