diff --git a/server/migrations/1650636333167-UserSavingsRecord.ts b/server/migrations/1650636333167-UserSavingsRecord.ts new file mode 100644 index 00000000..8138fd5a --- /dev/null +++ b/server/migrations/1650636333167-UserSavingsRecord.ts @@ -0,0 +1,18 @@ +import {MigrationInterface, QueryRunner} from "typeorm"; + +export class UserSavingsRecord1650636333167 implements MigrationInterface { + name = 'UserSavingsRecord1650636333167' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TYPE "user_savings_record_type_enum" AS ENUM('input', 'output')`); + await queryRunner.query(`CREATE TABLE "user_savings_record" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "amount" integer NOT NULL, "type" "user_savings_record_type_enum" NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "userId" uuid NOT NULL, CONSTRAINT "PK_f5f68a0ad3332f08e8adc70a4c6" PRIMARY KEY ("id")); COMMENT ON COLUMN "user_savings_record"."amount" IS 'Stored in base 100'`); + await queryRunner.query(`ALTER TABLE "user_savings_record" ADD CONSTRAINT "FK_b126b5220c39f0c684fd3929eac" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "user_savings_record" DROP CONSTRAINT "FK_b126b5220c39f0c684fd3929eac"`); + await queryRunner.query(`DROP TABLE "user_savings_record"`); + await queryRunner.query(`DROP TYPE "user_savings_record_type_enum"`); + } + +} diff --git a/server/src/Domain/HumanResource/Savings/UserSavingsRecord.entity.spec.ts b/server/src/Domain/HumanResource/Savings/UserSavingsRecord.entity.spec.ts new file mode 100644 index 00000000..f9151d8c --- /dev/null +++ b/server/src/Domain/HumanResource/Savings/UserSavingsRecord.entity.spec.ts @@ -0,0 +1,19 @@ +import { mock, instance } from 'ts-mockito'; +import { User } from '../User/User.entity'; +import { SavingsRecordType, UserSavingsRecord } from './UserSavingsRecord.entity'; + +describe('UserSavingsRecord.entity', () => { + it('testGetters', () => { + const user = mock(User); + const userSavingsRecord = new UserSavingsRecord( + 100000, + SavingsRecordType.INPUT, + instance(user) + ); + + expect(userSavingsRecord.getId()).toBe(undefined); + expect(userSavingsRecord.getAmount()).toBe(100000); + expect(userSavingsRecord.getUser()).toBe(instance(user)); + expect(userSavingsRecord.getType()).toBe(SavingsRecordType.INPUT); + }); +}); diff --git a/server/src/Domain/HumanResource/Savings/UserSavingsRecord.entity.ts b/server/src/Domain/HumanResource/Savings/UserSavingsRecord.entity.ts new file mode 100644 index 00000000..b3927d79 --- /dev/null +++ b/server/src/Domain/HumanResource/Savings/UserSavingsRecord.entity.ts @@ -0,0 +1,56 @@ +import { + Entity, + Column, + PrimaryGeneratedColumn, + ManyToOne +} from 'typeorm'; +import { User } from '../User/User.entity'; + +export enum SavingsRecordType { + INPUT = 'input', + OUTPUT = 'output' +} + +@Entity() +export class UserSavingsRecord { + @PrimaryGeneratedColumn('uuid') + private id: string; + + @Column({ type: 'integer', nullable: false, comment: 'Stored in base 100' }) + private amount: number; + + @Column('enum', { enum: SavingsRecordType, nullable: false }) + private type: SavingsRecordType; + + @ManyToOne(type => User, { nullable: false, onDelete: 'CASCADE' }) + private user: User; + + @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' }) + private createdAt: Date; + + constructor( + amount: number, + type: SavingsRecordType, + user: User, + ) { + this.amount = amount; + this.type = type; + this.user = user; + } + + public getId(): string { + return this.id; + } + + public getAmount(): number { + return this.amount; + } + + public getType(): SavingsRecordType { + return this.type; + } + + public getUser(): User { + return this.user; + } +} diff --git a/server/src/Infrastructure/HumanResource/humanResource.module.ts b/server/src/Infrastructure/HumanResource/humanResource.module.ts index 025349ad..5b75dedb 100644 --- a/server/src/Infrastructure/HumanResource/humanResource.module.ts +++ b/server/src/Infrastructure/HumanResource/humanResource.module.ts @@ -62,6 +62,7 @@ import { GetAvailableMealTicketsAction } from './MealTicket/Action/GetAvailableM import { GetMealTicketsPerMonthQueryHandler } from 'src/Application/HumanResource/MealTicket/Query/GetMealTicketsPerMonthQueryHandler'; import { UpdateLeaveRequestAction } from './Leave/Action/UpdateLeaveRequestAction'; import { UpdateLeaveRequestCommandHandler } from 'src/Application/HumanResource/Leave/Command/UpdateLeaveRequestCommandHandler'; +import { UserSavingsRecord } from 'src/Domain/HumanResource/Savings/UserSavingsRecord.entity'; @Module({ imports: [ @@ -75,7 +76,8 @@ import { UpdateLeaveRequestCommandHandler } from 'src/Application/HumanResource/ Leave, Event, Cooperative, - MealTicketRemoval + MealTicketRemoval, + UserSavingsRecord ]) ], controllers: [