UNPKG

phx-node

Version:

PHX NODE

202 lines 10 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 __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; 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 PHXNotificationByModuleService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.PHXNotificationByModuleService = void 0; const common_1 = require("@nestjs/common"); const core_1 = require("@nestjs/core"); const query_1 = require("../../query/notification/query"); const constant_1 = require("../../lib/constant"); const grpc_client_system_service_1 = require("../grpc-client/grpc-client-system.service"); const statistical_notification_service_1 = require("./statistical-notification.service"); const config_1 = require("@nestjs/config"); let PHXNotificationByModuleService = PHXNotificationByModuleService_1 = class PHXNotificationByModuleService { constructor(grpcClientSystemService, statisticalNotificationByModuleService, request, configService) { this.grpcClientSystemService = grpcClientSystemService; this.statisticalNotificationByModuleService = statisticalNotificationByModuleService; this.request = request; this.configService = configService; this.logger = new common_1.Logger(PHXNotificationByModuleService_1.name); } getListNotificationByAction(_a) { return __awaiter(this, arguments, void 0, function* ({ offset = 0, action, filter }) { const headers = this.request.headers; const userId = headers['authorization-id']; const deviceId = headers['device-id']; const app = headers['app']; const hostname = headers['hostname']; const response = yield this.grpcClientSystemService.queryDataCenter({ query: query_1.QUERY_GET_LIST_NOTIFICATION_BY_ACTION, variables: { app, device_id: deviceId, user_id: Number(userId), hostname, offset, limit: constant_1.LIMIT_RESPONSE_LIST_NOTIFICATION, action: action, filter }, }); const dataFeatureMobilePush = response.feature_mobile_push; return { data: dataFeatureMobilePush, last_page: dataFeatureMobilePush.length <= constant_1.LIMIT_RESPONSE_LIST_NOTIFICATION, }; }); } readNotification(_a) { return __awaiter(this, arguments, void 0, function* ({ notificationId }) { if (!notificationId) return; const headers = this.request.headers; const userId = headers['authorization-id']; const deviceId = headers['device-id']; const app = headers['app']; const hostname = headers['hostname']; return yield this.grpcClientSystemService .mutationDataCenter({ query: query_1.MUTATION_UPDATE_READ_NOTIFICATION, variables: { id: notificationId }, }) .then((response) => __awaiter(this, void 0, void 0, function* () { const { update_feature_mobile_push: { returning }, } = response; const readSuccess = returning.length > 0; if (!readSuccess) return response; const { type: notificationType, module_code, action, payload } = returning[0]; yield this.updateTotalNotificationAfterRead({ app, deviceId, userId, hostname, moduleCode: module_code, notificationType, action, payload }); return response; })); }); } updateTotalNotificationAfterRead(_a) { return __awaiter(this, arguments, void 0, function* ({ app, deviceId, userId, hostname, moduleCode, notificationType, action, payload }) { var _b; const studentCode = (_b = JSON.parse(payload)) === null || _b === void 0 ? void 0 : _b.student_code; const { currentTotalNotification, currentTotalNotificationImportant } = yield this.getCurrentTotalNotificationUnreadByModule({ app, deviceId, userId, hostname, moduleCode, action, studentCode }); yield this.statisticalNotificationByModuleService.updateStatisticalNotificationByModule({ userId, deviceId, hostname, moduleCode, action, studentCode, totalNotificationImportant: Math.max(0, notificationType === constant_1.NOTIFICATION_TYPE_IMPORTANT ? currentTotalNotificationImportant - 1 : currentTotalNotificationImportant), appName: app, totalNotification: Math.max(0, currentTotalNotification - 1) }); }); } getCurrentTotalNotificationUnreadByModule(_a) { return __awaiter(this, arguments, void 0, function* ({ app, deviceId, userId, hostname, moduleCode, studentCode, action }) { var _b; const response = yield this.grpcClientSystemService.queryDataCenter({ query: (0, query_1.QUERY_GET_STATISTICAL_NOTIFICATION_UNREAD_BY_MODULE)(studentCode), variables: { app, device_id: deviceId, user_id: userId, hostname, module_code: moduleCode, action }, }); const resultTotal = (_b = response === null || response === void 0 ? void 0 : response.feature_mobile_push_count_notification_unread) === null || _b === void 0 ? void 0 : _b[0]; return { currentTotalNotificationImportant: (resultTotal === null || resultTotal === void 0 ? void 0 : resultTotal.total_notification_important) || 0, currentTotalNotification: (resultTotal === null || resultTotal === void 0 ? void 0 : resultTotal.total_notification) || 0, }; }); } readNotificationInDetailForm(_a) { return __awaiter(this, arguments, void 0, function* ({ formId, action }) { if (!formId) return; const headers = this.request.headers; const userId = headers['authorization-id']; const deviceId = headers['device-id']; const app = headers['app']; const hostname = headers['hostname']; return yield this.grpcClientSystemService .mutationDataCenter({ query: query_1.MUTATION_UPDATE_READ_NOTIFICATION_IN_DETAIL_FORM, variables: { hostname, user_id: userId, app, device_id: deviceId, action, form_id: formId }, }) .then((response) => __awaiter(this, void 0, void 0, function* () { const { update_feature_mobile_push: { returning }, } = response; const readSuccess = returning.length > 0; if (!readSuccess) return response; const { type: notificationType, module_code, payload, action } = returning[0]; yield this.updateTotalNotificationAfterRead({ app, deviceId, userId, hostname, moduleCode: module_code, notificationType, action, payload }); return response; })); }); } }; exports.PHXNotificationByModuleService = PHXNotificationByModuleService; exports.PHXNotificationByModuleService = PHXNotificationByModuleService = PHXNotificationByModuleService_1 = __decorate([ (0, common_1.Injectable)(), __param(2, (0, common_1.Inject)(core_1.REQUEST)), __metadata("design:paramtypes", [grpc_client_system_service_1.PHXGrpcClientSystemService, statistical_notification_service_1.PHXStatisticalNotificationByModuleService, Request, config_1.ConfigService]) ], PHXNotificationByModuleService); //# sourceMappingURL=notification.service.js.map