@platform-modules/foreign-ministry 1.3.324 → 1.3.327
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.
- package/.env +10 -10
- package/dist/helpers/admin-auth.helper.d.ts +18 -0
- package/dist/helpers/admin-auth.helper.js +40 -0
- package/dist/helpers/services-notification-config.helper.d.ts +38 -0
- package/dist/helpers/services-notification-config.helper.js +181 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +10 -3
- package/dist/models/SlaApprovalsViewModel.js +47 -47
- package/dist/models/SlaMyRequestsViewModel.js +52 -52
- package/package.json +1 -1
- package/scripts/check-column-types.js +38 -38
- package/scripts/check-service-tables.js +42 -42
- package/scripts/sync-sla-reports-sql.js +95 -95
- package/src/helpers/admin-auth.helper.ts +46 -0
- package/src/helpers/services-notification-config.helper.ts +255 -0
- package/src/index.ts +14 -0
- package/src/models/SlaApprovalsViewModel.ts +135 -135
- package/src/models/SlaMyRequestsViewModel.ts +172 -172
- package/src/sla-report-views.ts +3 -3
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
const dotenv = require('dotenv');
|
|
2
|
-
const { Client } = require('pg');
|
|
3
|
-
|
|
4
|
-
dotenv.config({ path: '../Reports_Service/.env' });
|
|
5
|
-
dotenv.config({ path: '.env', override: true });
|
|
6
|
-
|
|
7
|
-
async function main() {
|
|
8
|
-
const client = new Client({
|
|
9
|
-
host: process.env.DB_HOST.trim(),
|
|
10
|
-
port: 5432,
|
|
11
|
-
user: process.env.DB_USER.trim(),
|
|
12
|
-
password: process.env.DB_PASS,
|
|
13
|
-
database: process.env.DB_NAME.trim(),
|
|
14
|
-
});
|
|
15
|
-
await client.connect();
|
|
16
|
-
|
|
17
|
-
const tables = ['sla_requests', 'users', 'departments', 'sections', 'fm_services', 'fm_sub_services'];
|
|
18
|
-
for (const table of tables) {
|
|
19
|
-
const { rows } = await client.query(
|
|
20
|
-
`SELECT column_name, data_type
|
|
21
|
-
FROM information_schema.columns
|
|
22
|
-
WHERE table_schema = 'public' AND table_name = $1
|
|
23
|
-
ORDER BY ordinal_position`,
|
|
24
|
-
[table]
|
|
25
|
-
);
|
|
26
|
-
console.log(`\n${table}:`);
|
|
27
|
-
for (const row of rows) {
|
|
28
|
-
console.log(` ${row.column_name}: ${row.data_type}`);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
await client.end();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
main().catch((err) => {
|
|
36
|
-
console.error(err.message);
|
|
37
|
-
process.exit(1);
|
|
38
|
-
});
|
|
1
|
+
const dotenv = require('dotenv');
|
|
2
|
+
const { Client } = require('pg');
|
|
3
|
+
|
|
4
|
+
dotenv.config({ path: '../Reports_Service/.env' });
|
|
5
|
+
dotenv.config({ path: '.env', override: true });
|
|
6
|
+
|
|
7
|
+
async function main() {
|
|
8
|
+
const client = new Client({
|
|
9
|
+
host: process.env.DB_HOST.trim(),
|
|
10
|
+
port: 5432,
|
|
11
|
+
user: process.env.DB_USER.trim(),
|
|
12
|
+
password: process.env.DB_PASS,
|
|
13
|
+
database: process.env.DB_NAME.trim(),
|
|
14
|
+
});
|
|
15
|
+
await client.connect();
|
|
16
|
+
|
|
17
|
+
const tables = ['sla_requests', 'users', 'departments', 'sections', 'fm_services', 'fm_sub_services'];
|
|
18
|
+
for (const table of tables) {
|
|
19
|
+
const { rows } = await client.query(
|
|
20
|
+
`SELECT column_name, data_type
|
|
21
|
+
FROM information_schema.columns
|
|
22
|
+
WHERE table_schema = 'public' AND table_name = $1
|
|
23
|
+
ORDER BY ordinal_position`,
|
|
24
|
+
[table]
|
|
25
|
+
);
|
|
26
|
+
console.log(`\n${table}:`);
|
|
27
|
+
for (const row of rows) {
|
|
28
|
+
console.log(` ${row.column_name}: ${row.data_type}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
await client.end();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
main().catch((err) => {
|
|
36
|
+
console.error(err.message);
|
|
37
|
+
process.exit(1);
|
|
38
|
+
});
|
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
const dotenv = require('dotenv');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const { Client } = require('pg');
|
|
4
|
-
|
|
5
|
-
async function listTables(label, config) {
|
|
6
|
-
const client = new Client(config);
|
|
7
|
-
await client.connect();
|
|
8
|
-
const { rows } = await client.query(`
|
|
9
|
-
SELECT table_name
|
|
10
|
-
FROM information_schema.tables
|
|
11
|
-
WHERE table_schema = 'public'
|
|
12
|
-
AND (table_name LIKE '%service%' OR table_name LIKE 'sla%')
|
|
13
|
-
ORDER BY table_name
|
|
14
|
-
`);
|
|
15
|
-
console.log(`${label} (${config.database}):`, rows.map((r) => r.table_name).join(', ') || '(none)');
|
|
16
|
-
await client.end();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async function main() {
|
|
20
|
-
dotenv.config({ path: path.join(__dirname, '..', '.env') });
|
|
21
|
-
await listTables('FM shared_models/.env', {
|
|
22
|
-
host: process.env.DB_HOST.trim(),
|
|
23
|
-
port: 5432,
|
|
24
|
-
user: process.env.DB_USER.trim(),
|
|
25
|
-
password: process.env.DB_PASS,
|
|
26
|
-
database: process.env.DB_NAME.trim(),
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
dotenv.config({ path: path.join(__dirname, '..', '..', 'Reports_Service', '.env') });
|
|
30
|
-
await listTables('Reports_Service/.env', {
|
|
31
|
-
host: (process.env.TYPEORM_HOST || '').trim(),
|
|
32
|
-
port: 5432,
|
|
33
|
-
user: (process.env.TYPEORM_USERNAME || '').trim(),
|
|
34
|
-
password: process.env.TYPEORM_PASSWORD,
|
|
35
|
-
database: (process.env.TYPEORM_DATABASE || '').trim(),
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
main().catch((err) => {
|
|
40
|
-
console.error(err.message);
|
|
41
|
-
process.exit(1);
|
|
42
|
-
});
|
|
1
|
+
const dotenv = require('dotenv');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { Client } = require('pg');
|
|
4
|
+
|
|
5
|
+
async function listTables(label, config) {
|
|
6
|
+
const client = new Client(config);
|
|
7
|
+
await client.connect();
|
|
8
|
+
const { rows } = await client.query(`
|
|
9
|
+
SELECT table_name
|
|
10
|
+
FROM information_schema.tables
|
|
11
|
+
WHERE table_schema = 'public'
|
|
12
|
+
AND (table_name LIKE '%service%' OR table_name LIKE 'sla%')
|
|
13
|
+
ORDER BY table_name
|
|
14
|
+
`);
|
|
15
|
+
console.log(`${label} (${config.database}):`, rows.map((r) => r.table_name).join(', ') || '(none)');
|
|
16
|
+
await client.end();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function main() {
|
|
20
|
+
dotenv.config({ path: path.join(__dirname, '..', '.env') });
|
|
21
|
+
await listTables('FM shared_models/.env', {
|
|
22
|
+
host: process.env.DB_HOST.trim(),
|
|
23
|
+
port: 5432,
|
|
24
|
+
user: process.env.DB_USER.trim(),
|
|
25
|
+
password: process.env.DB_PASS,
|
|
26
|
+
database: process.env.DB_NAME.trim(),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
dotenv.config({ path: path.join(__dirname, '..', '..', 'Reports_Service', '.env') });
|
|
30
|
+
await listTables('Reports_Service/.env', {
|
|
31
|
+
host: (process.env.TYPEORM_HOST || '').trim(),
|
|
32
|
+
port: 5432,
|
|
33
|
+
user: (process.env.TYPEORM_USERNAME || '').trim(),
|
|
34
|
+
password: process.env.TYPEORM_PASSWORD,
|
|
35
|
+
database: (process.env.TYPEORM_DATABASE || '').trim(),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
main().catch((err) => {
|
|
40
|
+
console.error(err.message);
|
|
41
|
+
process.exit(1);
|
|
42
|
+
});
|
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Applies SLA report views + stored procedures to PostgreSQL (idempotent CREATE OR REPLACE).
|
|
3
|
-
*
|
|
4
|
-
* Env (shared_models/.env or Reports_Service/.env):
|
|
5
|
-
* DB_HOST / TYPEORM_HOST
|
|
6
|
-
* DB_PORT / TYPEORM_PORT
|
|
7
|
-
* DB_USER / TYPEORM_USERNAME
|
|
8
|
-
* DB_PASS / TYPEORM_PASSWORD
|
|
9
|
-
* DB_NAME / TYPEORM_DATABASE
|
|
10
|
-
*
|
|
11
|
-
* Usage:
|
|
12
|
-
* node scripts/sync-sla-reports-sql.js
|
|
13
|
-
* npm run sync:sla-sql (from shared_models)
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
const fs = require('fs');
|
|
17
|
-
const path = require('path');
|
|
18
|
-
const { Client } = require('pg');
|
|
19
|
-
|
|
20
|
-
function loadEnvFiles() {
|
|
21
|
-
const dotenv = require('dotenv');
|
|
22
|
-
const sharedRoot = path.resolve(__dirname, '..');
|
|
23
|
-
const reportsEnv = path.resolve(sharedRoot, '..', 'Reports_Service', '.env');
|
|
24
|
-
const sharedEnv = path.join(sharedRoot, '.env');
|
|
25
|
-
|
|
26
|
-
// Reports_Service defaults first; shared_models/.env wins (FM DB for sync:sla-sql).
|
|
27
|
-
if (fs.existsSync(reportsEnv)) {
|
|
28
|
-
dotenv.config({ path: reportsEnv });
|
|
29
|
-
}
|
|
30
|
-
if (fs.existsSync(sharedEnv)) {
|
|
31
|
-
dotenv.config({ path: sharedEnv, override: true });
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function getDbConfig() {
|
|
36
|
-
const host = (process.env.TYPEORM_HOST || process.env.DB_HOST || '').trim();
|
|
37
|
-
const port = parseInt(process.env.TYPEORM_PORT || process.env.DB_PORT || '5432', 10);
|
|
38
|
-
const user = (process.env.TYPEORM_USERNAME || process.env.DB_USER || '').trim();
|
|
39
|
-
const password = process.env.TYPEORM_PASSWORD || process.env.DB_PASS || '';
|
|
40
|
-
const database = (process.env.TYPEORM_DATABASE || process.env.DB_NAME || '').trim();
|
|
41
|
-
|
|
42
|
-
if (!host || !user || !database) {
|
|
43
|
-
throw new Error(
|
|
44
|
-
'Missing DB config. Set TYPEORM_* in Reports_Service/.env or DB_* in shared_models/.env'
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return { host, port, user, password, database };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async function main() {
|
|
52
|
-
const skipSync = process.argv.includes('--skip');
|
|
53
|
-
if (skipSync || process.env.SKIP_SLA_SQL_SYNC === 'true') {
|
|
54
|
-
console.log('[sync-sla-reports-sql] skipped (SKIP_SLA_SQL_SYNC or --skip)');
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
loadEnvFiles();
|
|
59
|
-
const config = getDbConfig();
|
|
60
|
-
const sqlDir = path.join(__dirname, '..', 'sql');
|
|
61
|
-
const manifestPath = path.join(sqlDir, 'sla-reports-sync.manifest.json');
|
|
62
|
-
|
|
63
|
-
if (!fs.existsSync(manifestPath)) {
|
|
64
|
-
throw new Error(`Manifest not found: ${manifestPath}`);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const files = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
|
|
68
|
-
if (!Array.isArray(files) || files.length === 0) {
|
|
69
|
-
throw new Error('sla-reports-sync.manifest.json must be a non-empty array');
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const client = new Client(config);
|
|
73
|
-
await client.connect();
|
|
74
|
-
console.log(`[sync-sla-reports-sql] connected to ${config.host}/${config.database}`);
|
|
75
|
-
|
|
76
|
-
try {
|
|
77
|
-
for (const file of files) {
|
|
78
|
-
const filePath = path.join(sqlDir, file);
|
|
79
|
-
if (!fs.existsSync(filePath)) {
|
|
80
|
-
throw new Error(`SQL file missing: ${filePath}`);
|
|
81
|
-
}
|
|
82
|
-
const sql = fs.readFileSync(filePath, 'utf8');
|
|
83
|
-
await client.query(sql);
|
|
84
|
-
console.log(`[sync-sla-reports-sql] applied ${file}`);
|
|
85
|
-
}
|
|
86
|
-
console.log('[sync-sla-reports-sql] done');
|
|
87
|
-
} finally {
|
|
88
|
-
await client.end();
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
main().catch((err) => {
|
|
93
|
-
console.error('[sync-sla-reports-sql] failed:', err.message);
|
|
94
|
-
process.exit(1);
|
|
95
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* Applies SLA report views + stored procedures to PostgreSQL (idempotent CREATE OR REPLACE).
|
|
3
|
+
*
|
|
4
|
+
* Env (shared_models/.env or Reports_Service/.env):
|
|
5
|
+
* DB_HOST / TYPEORM_HOST
|
|
6
|
+
* DB_PORT / TYPEORM_PORT
|
|
7
|
+
* DB_USER / TYPEORM_USERNAME
|
|
8
|
+
* DB_PASS / TYPEORM_PASSWORD
|
|
9
|
+
* DB_NAME / TYPEORM_DATABASE
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
* node scripts/sync-sla-reports-sql.js
|
|
13
|
+
* npm run sync:sla-sql (from shared_models)
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const fs = require('fs');
|
|
17
|
+
const path = require('path');
|
|
18
|
+
const { Client } = require('pg');
|
|
19
|
+
|
|
20
|
+
function loadEnvFiles() {
|
|
21
|
+
const dotenv = require('dotenv');
|
|
22
|
+
const sharedRoot = path.resolve(__dirname, '..');
|
|
23
|
+
const reportsEnv = path.resolve(sharedRoot, '..', 'Reports_Service', '.env');
|
|
24
|
+
const sharedEnv = path.join(sharedRoot, '.env');
|
|
25
|
+
|
|
26
|
+
// Reports_Service defaults first; shared_models/.env wins (FM DB for sync:sla-sql).
|
|
27
|
+
if (fs.existsSync(reportsEnv)) {
|
|
28
|
+
dotenv.config({ path: reportsEnv });
|
|
29
|
+
}
|
|
30
|
+
if (fs.existsSync(sharedEnv)) {
|
|
31
|
+
dotenv.config({ path: sharedEnv, override: true });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function getDbConfig() {
|
|
36
|
+
const host = (process.env.TYPEORM_HOST || process.env.DB_HOST || '').trim();
|
|
37
|
+
const port = parseInt(process.env.TYPEORM_PORT || process.env.DB_PORT || '5432', 10);
|
|
38
|
+
const user = (process.env.TYPEORM_USERNAME || process.env.DB_USER || '').trim();
|
|
39
|
+
const password = process.env.TYPEORM_PASSWORD || process.env.DB_PASS || '';
|
|
40
|
+
const database = (process.env.TYPEORM_DATABASE || process.env.DB_NAME || '').trim();
|
|
41
|
+
|
|
42
|
+
if (!host || !user || !database) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
'Missing DB config. Set TYPEORM_* in Reports_Service/.env or DB_* in shared_models/.env'
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return { host, port, user, password, database };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function main() {
|
|
52
|
+
const skipSync = process.argv.includes('--skip');
|
|
53
|
+
if (skipSync || process.env.SKIP_SLA_SQL_SYNC === 'true') {
|
|
54
|
+
console.log('[sync-sla-reports-sql] skipped (SKIP_SLA_SQL_SYNC or --skip)');
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
loadEnvFiles();
|
|
59
|
+
const config = getDbConfig();
|
|
60
|
+
const sqlDir = path.join(__dirname, '..', 'sql');
|
|
61
|
+
const manifestPath = path.join(sqlDir, 'sla-reports-sync.manifest.json');
|
|
62
|
+
|
|
63
|
+
if (!fs.existsSync(manifestPath)) {
|
|
64
|
+
throw new Error(`Manifest not found: ${manifestPath}`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const files = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
|
|
68
|
+
if (!Array.isArray(files) || files.length === 0) {
|
|
69
|
+
throw new Error('sla-reports-sync.manifest.json must be a non-empty array');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const client = new Client(config);
|
|
73
|
+
await client.connect();
|
|
74
|
+
console.log(`[sync-sla-reports-sql] connected to ${config.host}/${config.database}`);
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
for (const file of files) {
|
|
78
|
+
const filePath = path.join(sqlDir, file);
|
|
79
|
+
if (!fs.existsSync(filePath)) {
|
|
80
|
+
throw new Error(`SQL file missing: ${filePath}`);
|
|
81
|
+
}
|
|
82
|
+
const sql = fs.readFileSync(filePath, 'utf8');
|
|
83
|
+
await client.query(sql);
|
|
84
|
+
console.log(`[sync-sla-reports-sql] applied ${file}`);
|
|
85
|
+
}
|
|
86
|
+
console.log('[sync-sla-reports-sql] done');
|
|
87
|
+
} finally {
|
|
88
|
+
await client.end();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
main().catch((err) => {
|
|
93
|
+
console.error('[sync-sla-reports-sql] failed:', err.message);
|
|
94
|
+
process.exit(1);
|
|
95
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { DataSource } from 'typeorm';
|
|
2
|
+
import { Role } from '../models/role';
|
|
3
|
+
import { UserRole } from '../models/userRolesModel';
|
|
4
|
+
|
|
5
|
+
/** Parse authenticated user id from Fastify `request.meta`. */
|
|
6
|
+
export function parsePortalUserIdFromRequest(request: {
|
|
7
|
+
meta?: { userId?: string | number };
|
|
8
|
+
}): number | null {
|
|
9
|
+
const raw = request.meta?.userId;
|
|
10
|
+
if (raw == null || raw === '') return null;
|
|
11
|
+
const n = Number(raw);
|
|
12
|
+
return Number.isFinite(n) && n > 0 ? n : null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* True when the user has an active Admin role in `user_role` (role.name = admin, case-insensitive).
|
|
17
|
+
* Same check as Reports_Service `isReportsAdmin`.
|
|
18
|
+
*/
|
|
19
|
+
export async function userHasPortalAdminRole(
|
|
20
|
+
dataSource: DataSource,
|
|
21
|
+
userId: number
|
|
22
|
+
): Promise<boolean> {
|
|
23
|
+
if (!dataSource?.isInitialized || !Number.isFinite(userId) || userId <= 0) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return dataSource
|
|
28
|
+
.getRepository(UserRole)
|
|
29
|
+
.createQueryBuilder('ur')
|
|
30
|
+
.innerJoin(Role, 'role', 'role.id = ur.role_id AND COALESCE(role.is_deleted, false) = false')
|
|
31
|
+
.where('ur.user_id = :userId', { userId })
|
|
32
|
+
.andWhere('ur.is_deleted = false')
|
|
33
|
+
.andWhere('ur.is_active = true')
|
|
34
|
+
.andWhere('LOWER(role.name) = :adminRole', { adminRole: 'admin' })
|
|
35
|
+
.getExists();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Convenience: resolve user id from request and check admin role. */
|
|
39
|
+
export async function isPortalAdminFromRequest(
|
|
40
|
+
dataSource: DataSource,
|
|
41
|
+
request: { meta?: { userId?: string | number } }
|
|
42
|
+
): Promise<boolean> {
|
|
43
|
+
const userId = parsePortalUserIdFromRequest(request);
|
|
44
|
+
if (!userId) return false;
|
|
45
|
+
return userHasPortalAdminRole(dataSource, userId);
|
|
46
|
+
}
|
|
@@ -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
|
@@ -425,6 +425,20 @@ export * from './models/EmployeeMilestoneDetailsModel';
|
|
|
425
425
|
export * from './models/MissionTravelPassportExpiryNotificationConfigModel';
|
|
426
426
|
export * from './models/ServicesNotificationConfigsModel';
|
|
427
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';
|
|
428
442
|
// Moodle Users Model
|
|
429
443
|
export * from './models/MoodleUsersModel';
|
|
430
444
|
// Evaluation
|