diff --git a/src/common/guards/modules.guard.ts b/src/common/guards/modules.guard.ts index b4654c5..936de2c 100644 --- a/src/common/guards/modules.guard.ts +++ b/src/common/guards/modules.guard.ts @@ -36,7 +36,7 @@ export class ModulesGuard implements CanActivate { for (const module of requiredModules) { if (!user.user_module_access.includes(module)) { throw new ForbiddenException( - `The role ${user.role} is not authorized to access this resource.`, + `This account does not have required access to: ${module}.`, ); } } diff --git a/src/identity-and-account/employees/services/employees-get.service.ts b/src/identity-and-account/employees/services/employees-get.service.ts index 7e6f330..aee9eaa 100644 --- a/src/identity-and-account/employees/services/employees-get.service.ts +++ b/src/identity-and-account/employees/services/employees-get.service.ts @@ -1,7 +1,7 @@ import { Injectable } from "@nestjs/common"; import { PrismaService } from "src/prisma/prisma.service"; -import { Modules } from "src/common/mappers/module-access.mapper"; +import { module_list, Modules } from "src/common/mappers/module-access.mapper"; import { EmailToIdResolver } from "src/common/mappers/email-id.mapper"; import { toStringFromDate } from "src/common/utils/date-utils"; import { Result } from "src/common/errors/result-error.factory"; @@ -175,7 +175,7 @@ export class EmployeesGetService { let module_access_array: Modules[] = []; if (employee.user.user_module_access) { - module_access_array = toKeysFromBoolean(employee.user.user_module_access); + module_access_array = toStringFromBoolean(employee.user.user_module_access); } const company_name = toStringFromCompanyCode(employee.company_code); @@ -201,4 +201,30 @@ export class EmployeesGetService { }, }; }; +} + +const createDefaultModuleAccess = (): Record => + module_list.reduce((acc, mod) => { + acc[mod] = false; + return acc; + }, {} as Record); + + +export const toBooleanFromString = (arr?: readonly string[] | null): Record => { + const result = createDefaultModuleAccess(); + if (!arr || !Array.isArray(arr)) return result; + for (const item of arr) { + if (typeof item !== 'string') continue; + const trimmed = item.trim(); + if ((module_list as readonly string[]).includes(trimmed)) { + result[trimmed as Modules] = true; + } + } + return result; +} + +export const toStringFromBoolean = (boolean_module_access: Record): Modules[] => { + const access_array = Object.entries(boolean_module_access); + const allowed_accesses = access_array.filter(([_key, value]) => value === true); + return allowed_accesses.map(([key]) => key as Modules); } \ No newline at end of file diff --git a/src/time-and-attendance/attachments/services/attachment-delete.service.ts b/src/time-and-attendance/attachments/services/attachment-delete.service.ts index a982931..c79d74d 100644 --- a/src/time-and-attendance/attachments/services/attachment-delete.service.ts +++ b/src/time-and-attendance/attachments/services/attachment-delete.service.ts @@ -1,4 +1,4 @@ -import { Injectable, NotFoundException } from "@nestjs/common"; +import { Injectable } from "@nestjs/common"; import { Result } from "src/common/errors/result-error.factory"; import { PrismaService } from "src/prisma/prisma.service"; @@ -15,8 +15,8 @@ export class AttachmentDeleteService { await tx.attachments.update({ where: { id: Number(id) }, data: { status: 'DELETED' } }); // decrement refcount - const dec = await tx.$executeRaw ` - UPDATE "Blobs" SET refcount = refcount - 1 + const dec = await tx.$executeRaw` + UPDATE "blobs" SET refcount = refcount - 1 WHERE sha256 = ${attachment.sha256} AND refcount > 0;`; return { ok: true, decremented: dec > 0 };