Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add employee field throughout files #267

Merged
merged 15 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 'zhang.iri@northeastern.edu',
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: 'zheng.kaiy@northeastern.edu',
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: 'weigl.a@northeastern.edu',
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: 'shirude.a@northeastern.edu',
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: 'info@mfa.org',
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: 'info@mfa.org',
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: 'john.doe@example.com',
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: 'bilbo.baggins@example.com',
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: 'john.doe@example.com',
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: 'bilbo.baggins@example.com',
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add the enum param on the api property?

@ApiProperty({enum: EmployeeScope})

or something similar?

this will also change the type that the auto-generated client has as well


@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