@platform-modules/foreign-ministry 1.3.322 → 1.3.326

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,255 @@
1
+ import type { EntityManager } from 'typeorm';
2
+ import { Notification, NotificationType } from '../models/NotificationModel';
3
+ import {
4
+ ServicesNotificationConfigs,
5
+ ServicesNotificationTriggerType,
6
+ } from '../models/ServicesNotificationConfigsModel';
7
+ import { UserRole } from '../models/userRolesModel';
8
+
9
+ export type FmServicesNotificationConfigRecipient = {
10
+ user_id: number;
11
+ department_id: number | null;
12
+ section_id: number | null;
13
+ role_id: number | null;
14
+ };
15
+
16
+ export type CollectFmServicesNotificationConfigRecipientsParams = {
17
+ serviceId: number | null | undefined;
18
+ subServiceId: number | null | undefined;
19
+ /** When 0, FINAL_APPROVAL configs apply; EVERY_APPROVAL always applies on approval. */
20
+ pendingCount: number;
21
+ logPrefix?: string;
22
+ };
23
+
24
+ export type SendFmServicesNotificationConfigNotificationsParams = {
25
+ serviceId: number | null | undefined;
26
+ subServiceId: number | null | undefined;
27
+ pendingCount: number;
28
+ requestId: number;
29
+ status: string;
30
+ approvalLevel: number;
31
+ shortProductName: string;
32
+ requestTypeKey: string;
33
+ routePath: string;
34
+ createdBy: string | number;
35
+ logPrefix?: string;
36
+ };
37
+
38
+ /**
39
+ * Resolves users from active `services_notification_configs` for service/sub-service.
40
+ * FINAL_APPROVAL only when pendingCount === 0; EVERY_APPROVAL on each approval step.
41
+ */
42
+ export async function collectFmServicesNotificationConfigRecipients(
43
+ manager: EntityManager,
44
+ params: CollectFmServicesNotificationConfigRecipientsParams
45
+ ): Promise<FmServicesNotificationConfigRecipient[]> {
46
+ const { serviceId, subServiceId, pendingCount, logPrefix = '[FM notification config]' } = params;
47
+ if (!serviceId || !subServiceId) return [];
48
+
49
+ const notificationConfigs = await manager
50
+ .getRepository(ServicesNotificationConfigs)
51
+ .createQueryBuilder('SNC')
52
+ .where('SNC.service_id = :service_id', { service_id: serviceId })
53
+ .andWhere('SNC.sub_service_id = :sub_service_id', { sub_service_id: subServiceId })
54
+ .andWhere('SNC.is_deleted = :is_deleted', { is_deleted: false })
55
+ .andWhere('SNC.is_active = :is_active', { is_active: true })
56
+ .getMany();
57
+
58
+ if (!notificationConfigs?.length) {
59
+ console.warn(
60
+ `${logPrefix} No notification configurations found for service ${serviceId} and sub_service ${subServiceId}`
61
+ );
62
+ return [];
63
+ }
64
+
65
+ const seen = new Map<number, FmServicesNotificationConfigRecipient>();
66
+
67
+ for (const notificationConfig of notificationConfigs) {
68
+ if (notificationConfig.trigger === ServicesNotificationTriggerType.FINAL_APPROVAL) {
69
+ if (pendingCount !== 0) continue;
70
+ } else if (notificationConfig.trigger === ServicesNotificationTriggerType.EVERY_APPROVAL) {
71
+ // include on every approval step
72
+ } else {
73
+ continue;
74
+ }
75
+
76
+ let usersQuery = manager
77
+ .getRepository(UserRole)
78
+ .createQueryBuilder('user_role')
79
+ .where('user_role.department_id = :department_id', {
80
+ department_id: notificationConfig.department_id,
81
+ })
82
+ .andWhere('user_role.section_id = :section_id', { section_id: notificationConfig.section_id })
83
+ .andWhere('user_role.is_deleted = :is_deleted', { is_deleted: false });
84
+
85
+ if (notificationConfig.role_id != null && notificationConfig.role_id !== undefined) {
86
+ usersQuery = usersQuery.andWhere('user_role.role_id = :role_id', {
87
+ role_id: notificationConfig.role_id,
88
+ });
89
+ }
90
+ const userRoles = await usersQuery.getMany();
91
+
92
+ if (!userRoles?.length) {
93
+ console.warn(
94
+ `${logPrefix} No users found for config (dept ${notificationConfig.department_id}${notificationConfig.section_id != null ? `, section ${notificationConfig.section_id}` : ''})`
95
+ );
96
+ continue;
97
+ }
98
+
99
+ for (const ur of userRoles) {
100
+ const uid = ur.user_id;
101
+ if (seen.has(uid)) continue;
102
+
103
+ const userRole = await manager
104
+ .getRepository(UserRole)
105
+ .createQueryBuilder('user_role')
106
+ .where('user_role.user_id = :userId', { userId: uid })
107
+ .andWhere('user_role.is_deleted = :is_deleted', { is_deleted: false })
108
+ .orderBy('user_role.created_at', 'ASC')
109
+ .getOne();
110
+
111
+ seen.set(uid, {
112
+ user_id: uid,
113
+ department_id: notificationConfig.department_id ?? null,
114
+ section_id: notificationConfig.section_id ?? null,
115
+ role_id: userRole?.role_id ?? null,
116
+ });
117
+ }
118
+ }
119
+
120
+ return Array.from(seen.values());
121
+ }
122
+
123
+ /**
124
+ * Inserts portal notifications for users resolved from `services_notification_configs`.
125
+ */
126
+ export async function sendFmServicesNotificationConfigNotifications(
127
+ manager: EntityManager,
128
+ params: SendFmServicesNotificationConfigNotificationsParams
129
+ ): Promise<{ sentCount: number }> {
130
+ const {
131
+ serviceId,
132
+ subServiceId,
133
+ pendingCount,
134
+ requestId,
135
+ status,
136
+ approvalLevel,
137
+ shortProductName,
138
+ requestTypeKey,
139
+ routePath,
140
+ createdBy,
141
+ logPrefix = '[FM notification config]',
142
+ } = params;
143
+
144
+ if (!serviceId || !subServiceId) return { sentCount: 0 };
145
+
146
+ const notificationConfigs = await manager
147
+ .getRepository(ServicesNotificationConfigs)
148
+ .createQueryBuilder('SNC')
149
+ .where('SNC.service_id = :service_id', { service_id: serviceId })
150
+ .andWhere('SNC.sub_service_id = :sub_service_id', { sub_service_id: subServiceId })
151
+ .andWhere('SNC.is_deleted = :is_deleted', { is_deleted: false })
152
+ .andWhere('SNC.is_active = :is_active', { is_active: true })
153
+ .getMany();
154
+
155
+ if (!notificationConfigs?.length) {
156
+ console.warn(
157
+ `${logPrefix} No notification configurations found for service ${serviceId} and sub_service ${subServiceId}`
158
+ );
159
+ return { sentCount: 0 };
160
+ }
161
+
162
+ let sentCount = 0;
163
+ const createdByStr = String(createdBy ?? 'system');
164
+
165
+ for (const notificationConfig of notificationConfigs) {
166
+ if (notificationConfig.trigger === ServicesNotificationTriggerType.FINAL_APPROVAL) {
167
+ if (pendingCount !== 0) continue;
168
+ } else if (notificationConfig.trigger === ServicesNotificationTriggerType.EVERY_APPROVAL) {
169
+ // include on every approval step
170
+ } else {
171
+ continue;
172
+ }
173
+
174
+ let usersQuery = manager
175
+ .getRepository(UserRole)
176
+ .createQueryBuilder('user_role')
177
+ .where('user_role.department_id = :department_id', {
178
+ department_id: notificationConfig.department_id,
179
+ })
180
+ .andWhere('user_role.section_id = :section_id', { section_id: notificationConfig.section_id })
181
+ .andWhere('user_role.is_deleted = :is_deleted', { is_deleted: false });
182
+
183
+ if (notificationConfig.role_id != null && notificationConfig.role_id !== undefined) {
184
+ usersQuery = usersQuery.andWhere('user_role.role_id = :role_id', {
185
+ role_id: notificationConfig.role_id,
186
+ });
187
+ }
188
+ const userRoles = await usersQuery.getMany();
189
+
190
+ if (!userRoles?.length) {
191
+ console.warn(
192
+ `${logPrefix} No users found for ${notificationConfig.trigger} config (dept ${notificationConfig.department_id}${notificationConfig.section_id != null ? `, section ${notificationConfig.section_id}` : ''})`
193
+ );
194
+ continue;
195
+ }
196
+
197
+ const isEveryApproval = notificationConfig.trigger === ServicesNotificationTriggerType.EVERY_APPROVAL;
198
+ const notificationTitle = isEveryApproval
199
+ ? `${shortProductName} Request ${status} at Level ${approvalLevel}`
200
+ : `${shortProductName} Request ${status}`;
201
+ const notificationData = `${shortProductName} request #${requestId} has been ${status}.`;
202
+
203
+ for (const ur of userRoles) {
204
+ const targetUserId = ur.user_id;
205
+ try {
206
+ const primaryUserRole = await manager
207
+ .getRepository(UserRole)
208
+ .createQueryBuilder('user_role')
209
+ .where('user_role.user_id = :userId', { userId: targetUserId })
210
+ .andWhere('user_role.is_deleted = :is_deleted', { is_deleted: false })
211
+ .orderBy('user_role.created_at', 'ASC')
212
+ .getOne();
213
+
214
+ await manager
215
+ .createQueryBuilder()
216
+ .insert()
217
+ .into(Notification)
218
+ .values({
219
+ type: NotificationType.REQUEST_RAISED,
220
+ user_id: targetUserId,
221
+ role_id: primaryUserRole?.role_id ?? null,
222
+ department_id: notificationConfig.department_id ?? null,
223
+ section_id: notificationConfig.section_id ?? null,
224
+ request_id: requestId,
225
+ service_id: serviceId ?? null,
226
+ sub_service_id: subServiceId ?? null,
227
+ content: {
228
+ title: notificationTitle,
229
+ data: notificationData,
230
+ requestId,
231
+ requestType: requestTypeKey,
232
+ status,
233
+ level: approvalLevel,
234
+ timestamp: new Date().toISOString(),
235
+ },
236
+ is_read: false,
237
+ route_path: routePath,
238
+ created_by: createdByStr,
239
+ } as never)
240
+ .execute();
241
+ sentCount += 1;
242
+ } catch (userNotificationError: unknown) {
243
+ const message =
244
+ userNotificationError instanceof Error ? userNotificationError.message : String(userNotificationError);
245
+ console.warn(`${logPrefix} Failed to send config notification to user ${targetUserId}:`, message);
246
+ }
247
+ }
248
+
249
+ console.info(
250
+ `${logPrefix} Sent ${notificationConfig.trigger} config notifications to ${userRoles.length} user(s) in department ${notificationConfig.department_id}${notificationConfig.section_id != null ? `, section ${notificationConfig.section_id}` : ''}`
251
+ );
252
+ }
253
+
254
+ return { sentCount };
255
+ }
package/src/index.ts CHANGED
@@ -98,8 +98,6 @@ export * from './models/UpdateAttendanceWorkflowModel';
98
98
  export * from './models/SlaRequestModel';
99
99
  export * from './models/ServiceSlaApprovalModel';
100
100
  export * from './models/SlaConfigModel';
101
- export * from './models/SlaMyRequestsViewModel';
102
- export * from './models/SlaApprovalsViewModel';
103
101
  export * from './models/GeneralServiceRequestsModel';
104
102
  export * from './models/GeneralServiceApprovalsModel';
105
103
  export * from './models/GeneralServiceWorkFlowModel';
@@ -427,6 +425,20 @@ export * from './models/EmployeeMilestoneDetailsModel';
427
425
  export * from './models/MissionTravelPassportExpiryNotificationConfigModel';
428
426
  export * from './models/ServicesNotificationConfigsModel';
429
427
  export { ServicesNotificationTriggerType } from './models/ServicesNotificationConfigsModel';
428
+ export {
429
+ collectFmServicesNotificationConfigRecipients,
430
+ sendFmServicesNotificationConfigNotifications,
431
+ } from './helpers/services-notification-config.helper';
432
+ export {
433
+ parsePortalUserIdFromRequest,
434
+ userHasPortalAdminRole,
435
+ isPortalAdminFromRequest,
436
+ } from './helpers/admin-auth.helper';
437
+ export type {
438
+ FmServicesNotificationConfigRecipient,
439
+ CollectFmServicesNotificationConfigRecipientsParams,
440
+ SendFmServicesNotificationConfigNotificationsParams,
441
+ } from './helpers/services-notification-config.helper';
430
442
  // Moodle Users Model
431
443
  export * from './models/MoodleUsersModel';
432
444
  // Evaluation
@@ -1,135 +1,135 @@
1
- import { ViewColumn, ViewEntity } from "typeorm";
2
-
3
- const VW_SLA_APPROVALS_SQL = `
4
- SELECT
5
- sa.id AS sla_approval_id,
6
- sa.source_approval_id,
7
- sa.request_id,
8
- sa.service_id AS service_id,
9
- sa.sub_service_id AS sub_service_id,
10
- TRIM(COALESCE(svc.name, ''))::TEXT AS service_name,
11
- TRIM(COALESCE(subsvc.sub_service_name, ''))::TEXT AS sub_service_name,
12
- sa.approver_role_id AS approval_role_id,
13
- TRIM(COALESCE(ar.name, ''))::TEXT AS approval_role_name,
14
- sa.department_id AS approval_department_id,
15
- TRIM(COALESCE(adpt.department_name, ''))::TEXT AS approval_department_name,
16
- sa.section_id AS approval_section_id,
17
- TRIM(COALESCE(asec.section_name, ''))::TEXT AS approval_section_name,
18
- sa.approval_status::TEXT AS approval_status,
19
- sr.status::TEXT AS request_status,
20
- sa.level AS level,
21
- sa.approver_user_id AS approver_user_id,
22
- TRIM(COALESCE(au.employee_name, ''))::TEXT AS approver_user_name,
23
- sa.delegate_user_id AS delegate_user_id,
24
- TRIM(COALESCE(du.employee_name, ''))::TEXT AS delegate_user_name,
25
- sa.approved_by AS approved_by,
26
- TRIM(COALESCE(ab.employee_name, ''))::TEXT AS approved_by_name,
27
- sa.comment::TEXT AS comment,
28
- sa.created_at AS created_at,
29
- sa.updated_at AS updated_at
30
- FROM sla_approval sa
31
- INNER JOIN sla_requests sr
32
- ON sr.request_id = sa.request_id AND COALESCE(sr.is_deleted, false) = false
33
- LEFT JOIN fm_services svc
34
- ON svc.id = sa.service_id AND COALESCE(svc.is_deleted, false) = false
35
- LEFT JOIN fm_sub_services subsvc
36
- ON subsvc.id = sa.sub_service_id AND COALESCE(subsvc.is_deleted, false) = false
37
- LEFT JOIN departments adpt
38
- ON adpt.id = sa.department_id AND COALESCE(adpt.is_deleted, false) = false
39
- LEFT JOIN sections asec
40
- ON asec.id = sa.section_id AND COALESCE(asec.is_deleted, false) = false
41
- LEFT JOIN role ar
42
- ON ar.id = sa.approver_role_id AND COALESCE(ar.is_deleted, false) = false
43
- LEFT JOIN users au
44
- ON au.id = sa.approver_user_id AND COALESCE(au.is_deleted, false) = false
45
- LEFT JOIN users du
46
- ON du.id = sa.delegate_user_id AND COALESCE(du.is_deleted, false) = false
47
- LEFT JOIN users ab
48
- ON ab.id = sa.approved_by AND COALESCE(ab.is_deleted, false) = false
49
- WHERE COALESCE(sa.is_deleted, false) = false
50
- `;
51
-
52
- /**
53
- * Read-only view for SLA approvals listings (TypeORM).
54
- * Approver matching (`sla_approval_matches_user` semantics) is applied in Reports_Service queries.
55
- */
56
- @ViewEntity({
57
- name: "vw_sla_approvals",
58
- expression: VW_SLA_APPROVALS_SQL,
59
- })
60
- export class SlaApprovalsView {
61
- @ViewColumn()
62
- sla_approval_id: number;
63
-
64
- @ViewColumn()
65
- source_approval_id: number;
66
-
67
- @ViewColumn()
68
- request_id: number;
69
-
70
- @ViewColumn()
71
- service_id: number;
72
-
73
- @ViewColumn()
74
- sub_service_id: number;
75
-
76
- @ViewColumn()
77
- service_name: string;
78
-
79
- @ViewColumn()
80
- sub_service_name: string;
81
-
82
- @ViewColumn()
83
- approval_role_id: number;
84
-
85
- @ViewColumn()
86
- approval_role_name: string;
87
-
88
- @ViewColumn()
89
- approval_department_id: number;
90
-
91
- @ViewColumn()
92
- approval_department_name: string;
93
-
94
- @ViewColumn()
95
- approval_section_id: number;
96
-
97
- @ViewColumn()
98
- approval_section_name: string;
99
-
100
- @ViewColumn()
101
- approval_status: string;
102
-
103
- @ViewColumn()
104
- request_status: string;
105
-
106
- @ViewColumn()
107
- level: number;
108
-
109
- @ViewColumn()
110
- approver_user_id: number;
111
-
112
- @ViewColumn()
113
- approver_user_name: string;
114
-
115
- @ViewColumn()
116
- delegate_user_id: number;
117
-
118
- @ViewColumn()
119
- delegate_user_name: string;
120
-
121
- @ViewColumn()
122
- approved_by: number;
123
-
124
- @ViewColumn()
125
- approved_by_name: string;
126
-
127
- @ViewColumn()
128
- comment: string;
129
-
130
- @ViewColumn()
131
- created_at: Date;
132
-
133
- @ViewColumn()
134
- updated_at: Date;
135
- }
1
+ import { ViewColumn, ViewEntity } from "typeorm";
2
+
3
+ const VW_SLA_APPROVALS_SQL = `
4
+ SELECT
5
+ sa.id AS sla_approval_id,
6
+ sa.source_approval_id,
7
+ sa.request_id,
8
+ sa.service_id AS service_id,
9
+ sa.sub_service_id AS sub_service_id,
10
+ TRIM(COALESCE(svc.name, ''))::TEXT AS service_name,
11
+ TRIM(COALESCE(subsvc.sub_service_name, ''))::TEXT AS sub_service_name,
12
+ sa.approver_role_id AS approval_role_id,
13
+ TRIM(COALESCE(ar.name, ''))::TEXT AS approval_role_name,
14
+ sa.department_id AS approval_department_id,
15
+ TRIM(COALESCE(adpt.department_name, ''))::TEXT AS approval_department_name,
16
+ sa.section_id AS approval_section_id,
17
+ TRIM(COALESCE(asec.section_name, ''))::TEXT AS approval_section_name,
18
+ sa.approval_status::TEXT AS approval_status,
19
+ sr.status::TEXT AS request_status,
20
+ sa.level AS level,
21
+ sa.approver_user_id AS approver_user_id,
22
+ TRIM(COALESCE(au.employee_name, ''))::TEXT AS approver_user_name,
23
+ sa.delegate_user_id AS delegate_user_id,
24
+ TRIM(COALESCE(du.employee_name, ''))::TEXT AS delegate_user_name,
25
+ sa.approved_by AS approved_by,
26
+ TRIM(COALESCE(ab.employee_name, ''))::TEXT AS approved_by_name,
27
+ sa.comment::TEXT AS comment,
28
+ sa.created_at AS created_at,
29
+ sa.updated_at AS updated_at
30
+ FROM sla_approval sa
31
+ INNER JOIN sla_requests sr
32
+ ON sr.request_id = sa.request_id AND COALESCE(sr.is_deleted, false) = false
33
+ LEFT JOIN fm_services svc
34
+ ON svc.id = sa.service_id AND COALESCE(svc.is_deleted, false) = false
35
+ LEFT JOIN fm_sub_services subsvc
36
+ ON subsvc.id = sa.sub_service_id AND COALESCE(subsvc.is_deleted, false) = false
37
+ LEFT JOIN departments adpt
38
+ ON adpt.id = sa.department_id AND COALESCE(adpt.is_deleted, false) = false
39
+ LEFT JOIN sections asec
40
+ ON asec.id = sa.section_id AND COALESCE(asec.is_deleted, false) = false
41
+ LEFT JOIN role ar
42
+ ON ar.id = sa.approver_role_id AND COALESCE(ar.is_deleted, false) = false
43
+ LEFT JOIN users au
44
+ ON au.id = sa.approver_user_id AND COALESCE(au.is_deleted, false) = false
45
+ LEFT JOIN users du
46
+ ON du.id = sa.delegate_user_id AND COALESCE(du.is_deleted, false) = false
47
+ LEFT JOIN users ab
48
+ ON ab.id = sa.approved_by AND COALESCE(ab.is_deleted, false) = false
49
+ WHERE COALESCE(sa.is_deleted, false) = false
50
+ `;
51
+
52
+ /**
53
+ * Read-only view for SLA approvals listings (TypeORM).
54
+ * Approver matching (`sla_approval_matches_user` semantics) is applied in Reports_Service queries.
55
+ */
56
+ @ViewEntity({
57
+ name: "vw_sla_approvals",
58
+ expression: VW_SLA_APPROVALS_SQL,
59
+ })
60
+ export class SlaApprovalsView {
61
+ @ViewColumn()
62
+ sla_approval_id: number;
63
+
64
+ @ViewColumn()
65
+ source_approval_id: number;
66
+
67
+ @ViewColumn()
68
+ request_id: number;
69
+
70
+ @ViewColumn()
71
+ service_id: number;
72
+
73
+ @ViewColumn()
74
+ sub_service_id: number;
75
+
76
+ @ViewColumn()
77
+ service_name: string;
78
+
79
+ @ViewColumn()
80
+ sub_service_name: string;
81
+
82
+ @ViewColumn()
83
+ approval_role_id: number;
84
+
85
+ @ViewColumn()
86
+ approval_role_name: string;
87
+
88
+ @ViewColumn()
89
+ approval_department_id: number;
90
+
91
+ @ViewColumn()
92
+ approval_department_name: string;
93
+
94
+ @ViewColumn()
95
+ approval_section_id: number;
96
+
97
+ @ViewColumn()
98
+ approval_section_name: string;
99
+
100
+ @ViewColumn()
101
+ approval_status: string;
102
+
103
+ @ViewColumn()
104
+ request_status: string;
105
+
106
+ @ViewColumn()
107
+ level: number;
108
+
109
+ @ViewColumn()
110
+ approver_user_id: number;
111
+
112
+ @ViewColumn()
113
+ approver_user_name: string;
114
+
115
+ @ViewColumn()
116
+ delegate_user_id: number;
117
+
118
+ @ViewColumn()
119
+ delegate_user_name: string;
120
+
121
+ @ViewColumn()
122
+ approved_by: number;
123
+
124
+ @ViewColumn()
125
+ approved_by_name: string;
126
+
127
+ @ViewColumn()
128
+ comment: string;
129
+
130
+ @ViewColumn()
131
+ created_at: Date;
132
+
133
+ @ViewColumn()
134
+ updated_at: Date;
135
+ }