Skip to content

Commit

Permalink
add employee field throughout files (#267)
Browse files Browse the repository at this point in the history
* add employee field throughout files

Co-authored-by: Gayatri Kondabathini <[email protected]>

* replace isAdmin with scope

* create migration for employee scope field

* add scope to completeRegistration signature

* fix format and tsc checks

* add employee field throughout files

Co-authored-by: Gayatri Kondabathini <[email protected]>

* replace isAdmin with scope

* create migration for employee scope field

* add scope to completeRegistration signature

* fix format and tsc checks

* change api properties to enum and update completeRegistration signature

* use correct scope type from client

* fix tsc type in context type

---------

Co-authored-by: Gayatri Kondabathini <[email protected]>
Co-authored-by: Kaiyang Zheng <[email protected]>
  • Loading branch information
3 people authored Feb 9, 2025
1 parent 9449716 commit b5009f3
Show file tree
Hide file tree
Showing 19 changed files with 107 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:
- You are about to drop the column `isAdmin` on the `Employee` table. All the data in the column will be lost.
*/
-- CreateEnum
CREATE TYPE "EmployeeScope" AS ENUM ('BASE_USER', 'ADMIN');

-- AlterTable
ALTER TABLE "Employee" DROP COLUMN "isAdmin",
ADD COLUMN "scope" "EmployeeScope" NOT NULL DEFAULT 'BASE_USER';
7 changes: 6 additions & 1 deletion apps/server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ enum SignerType {
USER_LIST
}

enum EmployeeScope {
BASE_USER
ADMIN
}

// `Departments` represent the various departments that employees could work in.
model Department {
id String @id @default(uuid()) @db.Uuid
Expand All @@ -39,7 +44,7 @@ model Employee {
lastName String @db.VarChar(255)
email String @unique @db.VarChar(255)
signatureLink String @db.VarChar(255)
isAdmin Boolean @default(false) @db.Boolean
scope EmployeeScope @default(BASE_USER)
pswdHash String? @db.VarChar(255)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down
8 changes: 7 additions & 1 deletion apps/server/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrismaClient, SignerType } from '@prisma/client';
import { PrismaClient, SignerType, EmployeeScope } from '@prisma/client';
import { v4 as uuidv4 } from 'uuid';

const prisma = new PrismaClient();
Expand Down Expand Up @@ -49,6 +49,7 @@ type EmployeeData = {
email: string;
positionId: string;
signatureLink: string;
scope: EmployeeScope;
};

// update or insert employee to database based on the employee id
Expand All @@ -65,6 +66,7 @@ async function upsertEmployee(empData: EmployeeData) {
position: {
connect: { id: empData.positionId },
},
scope: empData.scope,
},
});
}
Expand Down Expand Up @@ -389,6 +391,7 @@ async function main() {
email: '[email protected]',
positionId: CHIEF_OF_STAFF_UUID,
signatureLink: DEV_SIGNATURE_LINK,
scope: EmployeeScope.BASE_USER,
},
{
id: KAI_ZHENG_UUID,
Expand All @@ -397,6 +400,7 @@ async function main() {
email: '[email protected]',
positionId: CHIEF_FIN_OFFICER_UUID,
signatureLink: DEV_SIGNATURE_LINK,
scope: EmployeeScope.BASE_USER,
},
{
id: ANGELA_WEIGL_UUID,
Expand All @@ -405,6 +409,7 @@ async function main() {
email: '[email protected]',
positionId: AGG_DIR_UUID,
signatureLink: DEV_SIGNATURE_LINK,
scope: EmployeeScope.BASE_USER,
},
{
id: ANSHUL_SHIRUDE_UUID,
Expand All @@ -413,6 +418,7 @@ async function main() {
email: '[email protected]',
positionId: CHIEF_LEARNING_ENGAGEMENT_UUID,
signatureLink: DEV_SIGNATURE_LINK,
scope: EmployeeScope.BASE_USER,
},
];

Expand Down
1 change: 1 addition & 0 deletions apps/server/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export class AppController {
password: employeeDto.password,
signatureLink: employeeDto.signatureLink,
positionId: '',
scope: employeeDto.scope,
};

const newEmployee = await this.authService.register(
Expand Down
7 changes: 4 additions & 3 deletions apps/server/src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { EmployeeEntity } from '../employees/entities/employee.entity';
import { PositionBaseEntity } from '../positions/entities/position.entity';
import { DepartmentsService } from '../departments/departments.service';
import { PositionsService } from '../positions/positions.service';
import { EmployeeScope } from '@prisma/client';

describe('AuthService', () => {
let service: AuthService;
Expand Down Expand Up @@ -60,7 +61,7 @@ describe('AuthService', () => {
updatedAt: new Date(1672531200),
},
email: '[email protected]',
isAdmin: false,
scope: EmployeeScope.BASE_USER,
pswdHash: 'password',
createdAt: new Date(1672531200),
updatedAt: new Date(1672531200),
Expand Down Expand Up @@ -89,7 +90,7 @@ describe('AuthService', () => {
updatedAt: new Date(1672531200),
},
email: '[email protected]',
isAdmin: false,
scope: EmployeeScope.BASE_USER,
createdAt: new Date(1672531200),
updatedAt: new Date(1672531200),
refreshToken: null,
Expand Down Expand Up @@ -147,7 +148,7 @@ describe('AuthService', () => {
firstName: 'First',
lastName: 'Last',
positionId: 'position-id',
isAdmin: false,
scope: EmployeeScope.BASE_USER,
pswdHash: null,
createdAt: new Date(0),
updatedAt: new Date(0),
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class AuthService {
sub: user.id,
positionId: user.positionId,
departmentId: user.position.departmentId,
isAdmin: user.isAdmin,
scope: user.scope,
};

const [accessToken, refreshToken] = await Promise.all([
Expand Down
5 changes: 5 additions & 0 deletions apps/server/src/auth/dto/register-employee.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { EmployeeScope } from '@prisma/client';
import { IsNotEmpty, IsString, MinLength, IsEmail } from 'class-validator';

export class RegisterEmployeeDto {
Expand Down Expand Up @@ -37,4 +38,8 @@ export class RegisterEmployeeDto {
@IsNotEmpty()
@ApiProperty()
signatureLink: string;

@IsNotEmpty()
@ApiProperty({ enum: EmployeeScope })
scope: EmployeeScope;
}
3 changes: 2 additions & 1 deletion apps/server/src/auth/guards/admin-auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
UnauthorizedException,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { EmployeeScope } from '@prisma/client';
import { Request } from 'express';

@Injectable()
Expand All @@ -23,7 +24,7 @@ export class AdminAuthGuard extends AuthGuard('jwt') {

handleRequest(err: any, user: any, info: any) {
// You can throw an exception based on either "info" or "err" arguments
if (err || !user || !user.isAdmin) {
if (err || !user || !(user.scope == EmployeeScope.ADMIN)) {
console.log(info);
throw err || new UnauthorizedException();
}
Expand Down
6 changes: 6 additions & 0 deletions apps/server/src/employees/dto/create-employee.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { EmployeeScope } from '@prisma/client';
import {
IsEmail,
IsNotEmpty,
Expand Down Expand Up @@ -38,4 +39,9 @@ export class CreateEmployeeDto {
@IsNotEmpty()
@ApiProperty()
signatureLink: string;

@IsString()
@IsNotEmpty()
@ApiProperty({ enum: EmployeeScope })
scope: EmployeeScope;
}
9 changes: 5 additions & 4 deletions apps/server/src/employees/employees.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EmployeesService } from './employees.service';
import { PrismaService } from '../prisma/prisma.service';
import { EmployeeEntity } from './entities/employee.entity';
import { LoggerServiceImpl } from '../logger/logger.service';
import { EmployeeScope } from '@prisma/client';

describe('EmployeesController', () => {
let controller: EmployeesController;
Expand Down Expand Up @@ -47,7 +48,7 @@ describe('EmployeesController', () => {
},
},
email: '[email protected]',
isAdmin: false,
scope: EmployeeScope.BASE_USER,
pswdHash: 'thisIsASecureHash',
createdAt: new Date(1672531200),
updatedAt: new Date(1672531200),
Expand All @@ -74,7 +75,7 @@ describe('EmployeesController', () => {
},
},
email: '[email protected]',
isAdmin: false,
scope: EmployeeScope.BASE_USER,
pswdHash: 'thisIsASecureHash',
createdAt: new Date(1672531200),
updatedAt: new Date(1672531200),
Expand Down Expand Up @@ -104,7 +105,7 @@ describe('EmployeesController', () => {
},
},
email: '[email protected]',
isAdmin: false,
scope: EmployeeScope.BASE_USER,
pswdHash: 'thisIsASecureHash',
createdAt: new Date(1672531200),
updatedAt: new Date(1672531200),
Expand All @@ -131,7 +132,7 @@ describe('EmployeesController', () => {
},
},
email: '[email protected]',
isAdmin: false,
scope: EmployeeScope.BASE_USER,
pswdHash: 'thisIsASecureHash',
createdAt: new Date(1672531200),
updatedAt: new Date(1672531200),
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/employees/employees.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class EmployeesService {
createEmployeeDto.password,
await bcrypt.genSalt(),
),
scope: createEmployeeDto.scope,
},
include: {
position: {
Expand Down
8 changes: 4 additions & 4 deletions apps/server/src/employees/entities/employee.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { Employee } from '@prisma/client';
import { Employee, EmployeeScope } from '@prisma/client';
import { PositionBaseEntity } from './../../positions/entities/position.entity';
import { Exclude } from 'class-transformer';

Expand All @@ -19,12 +19,12 @@ export class EmployeeBaseEntity implements Employee {
@ApiProperty()
email: string;

@ApiProperty()
isAdmin: boolean;

@ApiProperty()
signatureLink: string;

@ApiProperty({ enum: EmployeeScope })
scope: EmployeeScope;

@Exclude()
pswdHash: string | null;

Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EmployeeScope } from '@prisma/client';

/* eslint-disable */
export default async () => {
const t = {
Expand Down Expand Up @@ -107,6 +109,7 @@ export default async () => {
positionId: { required: true, type: () => String },
email: { required: true, type: () => String },
password: { required: true, type: () => String, minLength: 5 },
scope: { required: true, type: () => EmployeeScope },
},
},
],
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/signatures/signatures.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PositionsService } from '../positions/positions.service';
import { UpdateSignatureSignerDto } from './dto/update-signature-signer.dto';
import { SignerType } from '@prisma/client';

let mockSignatureService = {
const mockSignatureService = {
updateSigner: async (
signatureId: string,
updateSignatureSignerDto: UpdateSignatureSignerDto,
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('SignaturesController', () => {
});

it('should update signer', async () => {
let response = await controller.updateSignatureSigner('signature-id', {
const response = await controller.updateSignatureSigner('signature-id', {
signerType: SignerType.DEPARTMENT,
signerDepartmentId: 'department-id',
});
Expand Down
32 changes: 24 additions & 8 deletions apps/web/src/client/schemas.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export const RegisterEmployeeDtoSchema = {
signatureLink: {
type: 'string',
},
scope: {
type: 'string',
enum: ['BASE_USER', 'ADMIN'],
},
},
required: [
'firstName',
Expand All @@ -44,6 +48,7 @@ export const RegisterEmployeeDtoSchema = {
'positionName',
'departmentName',
'signatureLink',
'scope',
],
} as const;

Expand Down Expand Up @@ -121,12 +126,13 @@ export const EmployeeEntitySchema = {
email: {
type: 'string',
},
isAdmin: {
type: 'boolean',
},
signatureLink: {
type: 'string',
},
scope: {
type: 'string',
enum: ['BASE_USER', 'ADMIN'],
},
position: {
$ref: '#/components/schemas/PositionBaseEntity',
},
Expand Down Expand Up @@ -155,8 +161,8 @@ export const EmployeeEntitySchema = {
'firstName',
'lastName',
'email',
'isAdmin',
'signatureLink',
'scope',
'position',
'positionId',
'pswdHash',
Expand Down Expand Up @@ -188,6 +194,10 @@ export const CreateEmployeeDtoSchema = {
signatureLink: {
type: 'string',
},
scope: {
type: 'string',
enum: ['BASE_USER', 'ADMIN'],
},
},
required: [
'firstName',
Expand All @@ -196,6 +206,7 @@ export const CreateEmployeeDtoSchema = {
'email',
'password',
'signatureLink',
'scope',
],
} as const;

Expand All @@ -214,6 +225,10 @@ export const UpdateEmployeeDtoSchema = {
signatureLink: {
type: 'string',
},
scope: {
type: 'string',
enum: ['BASE_USER', 'ADMIN'],
},
},
} as const;

Expand Down Expand Up @@ -245,12 +260,13 @@ export const EmployeeBaseEntitySchema = {
email: {
type: 'string',
},
isAdmin: {
type: 'boolean',
},
signatureLink: {
type: 'string',
},
scope: {
type: 'string',
enum: ['BASE_USER', 'ADMIN'],
},
positionId: {
type: 'string',
},
Expand All @@ -276,8 +292,8 @@ export const EmployeeBaseEntitySchema = {
'firstName',
'lastName',
'email',
'isAdmin',
'signatureLink',
'scope',
'positionId',
'pswdHash',
'createdAt',
Expand Down
Loading

0 comments on commit b5009f3

Please sign in to comment.