mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-07 22:25:42 +00:00
feat(freight-me): add position type lookup and enrich user profile with position type
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { InjectDataSource } from '@nestjs/typeorm';
|
||||||
import type { TCurrentUser } from '@tria-plc/api-common/modules/auth/types/current-user.type';
|
import type { TCurrentUser } from '@tria-plc/api-common/modules/auth/types/current-user.type';
|
||||||
|
import { DataSource } from 'typeorm';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
collectPermissionKeys,
|
collectPermissionKeys,
|
||||||
@@ -9,7 +11,35 @@ import { PERMISSIONS_CATALOG } from '../../seed/freight-permissions.registry';
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class FreightMeService {
|
export class FreightMeService {
|
||||||
getEnrichedProfile(user: TCurrentUser) {
|
constructor(@InjectDataSource() private readonly dataSource: DataSource) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The JWT session snapshot has no position TYPE, but the backoffice needs it
|
||||||
|
* (GL sub-positions are identified by type key). Resolved live from IAM.
|
||||||
|
*/
|
||||||
|
private async lookupPositionType(
|
||||||
|
positionId: string | undefined,
|
||||||
|
): Promise<{ key: string; name: unknown } | null> {
|
||||||
|
if (!positionId) return null;
|
||||||
|
try {
|
||||||
|
const rows: { key: string; name: unknown }[] = await this.dataSource.query(
|
||||||
|
`SELECT pt.key, pt.name
|
||||||
|
FROM iam.positions p
|
||||||
|
JOIN iam.position_types pt ON pt.id = p.position_type_id
|
||||||
|
WHERE p.id = $1`,
|
||||||
|
[positionId],
|
||||||
|
);
|
||||||
|
return rows[0] ?? null;
|
||||||
|
} catch {
|
||||||
|
return null; // iam schema unreachable — degrade to the old payload shape
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getEnrichedProfile(user: TCurrentUser) {
|
||||||
|
const positionType = await this.lookupPositionType(
|
||||||
|
user.employee?.position?.id,
|
||||||
|
);
|
||||||
|
|
||||||
const employee = user.employee
|
const employee = user.employee
|
||||||
? [
|
? [
|
||||||
{
|
{
|
||||||
@@ -27,6 +57,7 @@ export class FreightMeService {
|
|||||||
isDelegate: user.employee.position.isDelegate,
|
isDelegate: user.employee.position.isDelegate,
|
||||||
parentPositionId: user.employee.position.parentPositionId,
|
parentPositionId: user.employee.position.parentPositionId,
|
||||||
permissions: user.employee.position.permissions ?? [],
|
permissions: user.employee.position.permissions ?? [],
|
||||||
|
positionType,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
: [],
|
: [],
|
||||||
|
|||||||
Reference in New Issue
Block a user