@things-factory/organization 10.1.181 → 10.1.182
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.
|
@@ -23,9 +23,17 @@ async function registerEmployeeAsSystemUser(employeeId, context) {
|
|
|
23
23
|
if (!employee.contact) {
|
|
24
24
|
throw new Error(context.t('error.contact-information-required'));
|
|
25
25
|
}
|
|
26
|
+
/*
|
|
27
|
+
* A role named as the employee's job responsibility comes along when the domain has one — as a job change swaps it
|
|
28
|
+
* below. A domain's roles need not follow its job titles, and one that does not have such a role still registers
|
|
29
|
+
* the employee; the roles are then given on the role screen.
|
|
30
|
+
*/
|
|
31
|
+
const sameNamed = employee.active && employee.jobResponsibility
|
|
32
|
+
? await (0, shell_1.getRepository)(auth_base_1.Role, tx).findOne({ where: { name: employee.jobResponsibility, domain: { id: domain.id } } })
|
|
33
|
+
: null;
|
|
26
34
|
const systemUser = await (0, contact_1.registerContactAsSystemUser)({
|
|
27
35
|
contactId: employee.contact.id,
|
|
28
|
-
roleName:
|
|
36
|
+
roleName: sameNamed?.name
|
|
29
37
|
}, context);
|
|
30
38
|
employee.user = systemUser;
|
|
31
39
|
await employeeRepository.save(employee);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"register-employee-as-system-user.js","sourceRoot":"","sources":["../../server/controllers/register-employee-as-system-user.ts"],"names":[],"mappings":";;AAOA,
|
|
1
|
+
{"version":3,"file":"register-employee-as-system-user.js","sourceRoot":"","sources":["../../server/controllers/register-employee-as-system-user.ts"],"names":[],"mappings":";;AAOA,oEA4CC;AAED,oEAsDC;AAED,oEAqCC;AAlJD,iDAAqD;AACrD,yDAAsD;AACtD,qDAAqE;AAErE,iEAA0D;AAGnD,KAAK,UAAU,4BAA4B,CAAC,UAAkB,EAAE,OAAwB;IAC7F,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,KAAK,CAAA;IAEpC,MAAM,kBAAkB,GAAG,IAAA,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E;SAC1B;QACD,SAAS,EAAE,CAAC,MAAM,CAAC;KACpB,CAAC,CAAA;IAEF,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;QACnB,OAAM;IACR,CAAC;IAED,MAAM,EAAE,iBAAiB,EAAE,GAAG,QAAQ,CAAA;IAEtC,IAAI,iBAAiB,EAAE,CAAC;QACtB,MAAM,cAAc,GAAG,IAAA,qBAAa,EAAC,gBAAI,EAAE,EAAE,CAAC,CAAA;QAE9C,MAAM,UAAU,GAAG,MAAM,cAAc,CAAC,OAAO,CAAC;YAC9C,KAAK,EAAE,EAAE,EAAE,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,EAAE;YAC/B,SAAS,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC;SAChC,CAAC,CAAA;QAEF,MAAM,QAAQ,GAAG,MAAM,IAAA,qBAAa,EAAC,gBAAI,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC;YACrD,KAAK,EAAE,EAAE,IAAI,EAAE,iBAAiB,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,EAAE,EAAE;SAC9D,CAAC,CAAA;QAEF,IAAI,QAAQ,EAAE,CAAC;YACb,UAAU,CAAC,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,KAAK,QAAQ,CAAC,EAAE,CAAC,CAAA;YAC3E,UAAU,CAAC,OAAO,GAAG,IAAI,CAAA;YACzB,MAAM,cAAc,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;QACvC,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import { getRepository } from '@things-factory/shell'\nimport { Role, User } from '@things-factory/auth-base'\nimport { registerContactAsSystemUser } from '@things-factory/contact'\n\nimport { Employee } from '../service/employee/employee.js'\nimport { EmployeePatch } from '../service/employee/employee-type.js'\n\nexport async function registerEmployeeAsSystemUser(employeeId: string, context: ResolverContext) {\n const { domain, tx } = context.state\n\n const employeeRepository = getRepository(Employee, tx)\n\n const employee = await employeeRepository.findOne({\n where: {\n id: employeeId,\n domain: { id: domain.id }\n },\n relations: ['contact', 'user']\n })\n\n if (employee.user) {\n throw new Error(context.t('error.employee-already-registered'))\n }\n\n if (!employee.contact) {\n throw new Error(context.t('error.contact-information-required'))\n }\n\n /*\n * A role named as the employee's job responsibility comes along when the domain has one — as a job change swaps it\n * below. A domain's roles need not follow its job titles, and one that does not have such a role still registers\n * the employee; the roles are then given on the role screen.\n */\n const sameNamed =\n employee.active && employee.jobResponsibility\n ? await getRepository(Role, tx).findOne({ where: { name: employee.jobResponsibility, domain: { id: domain.id } } })\n : null\n\n const systemUser = await registerContactAsSystemUser(\n {\n contactId: employee.contact.id,\n roleName: sameNamed?.name\n },\n context\n )\n\n employee.user = systemUser\n\n await employeeRepository.save(employee)\n\n return systemUser\n}\n\nexport async function onUpdateEmployeeAsSystemUser(patch: EmployeePatch, context: ResolverContext) {\n const { domain, user, tx } = context.state\n const { id, jobResponsibility } = patch\n\n const employeeRepository = getRepository(Employee, tx)\n\n const employee = await employeeRepository.findOne({\n where: {\n id,\n domain: { id: domain.id }\n },\n relations: ['contact', 'user']\n })\n\n if (!employee.user) {\n return\n }\n\n const { jobResponsibility: lastJobResponsibility } = employee\n\n if ('jobResponsibility' in patch && lastJobResponsibility !== jobResponsibility) {\n const userRepository = getRepository(User, tx)\n\n const systemUser = await userRepository.findOne({\n where: { id: employee.user.id },\n relations: ['domains', 'roles']\n })\n\n const { roles } = systemUser\n let newRoles = [...roles]\n\n if (lastJobResponsibility) {\n const lastRole = await getRepository(Role, tx).findOne({\n where: { name: lastJobResponsibility, domain: { id: domain.id } }\n })\n\n if (lastRole) {\n newRoles = roles.filter(role => role.id !== lastRole.id)\n }\n }\n\n const newRole = await getRepository(Role, tx).findOne({\n where: { name: jobResponsibility, domain: { id: domain.id } }\n })\n\n if (newRole && !roles.find(role => role.id === newRole.id)) {\n newRoles = [...newRoles, newRole]\n }\n\n systemUser.roles = newRoles\n systemUser.updater = user\n\n await userRepository.save(systemUser)\n }\n}\n\nexport async function onDeleteEmployeeAsSystemUser(employeeId: string, context: ResolverContext) {\n const { domain, user, tx } = context.state\n\n const employeeRepository = getRepository(Employee, tx)\n\n const employee = await employeeRepository.findOne({\n where: {\n id: employeeId,\n domain: { id: domain.id }\n },\n relations: ['user']\n })\n\n if (!employee.user) {\n return\n }\n\n const { jobResponsibility } = employee\n\n if (jobResponsibility) {\n const userRepository = getRepository(User, tx)\n\n const systemUser = await userRepository.findOne({\n where: { id: employee.user.id },\n relations: ['domains', 'roles']\n })\n\n const lastRole = await getRepository(Role, tx).findOne({\n where: { name: jobResponsibility, domain: { id: domain.id } }\n })\n\n if (lastRole) {\n systemUser.roles = systemUser.roles.filter(role => role.id !== lastRole.id)\n systemUser.updater = user\n await userRepository.save(systemUser)\n }\n }\n}\n"]}
|