Skip to content

Commit

Permalink
Merge pull request #254 from fairnesscoop/feat/add-user-savings-recor…
Browse files Browse the repository at this point in the history
…d-model

Add UserSavingsRecord model
  • Loading branch information
mmarchois authored Apr 22, 2022
2 parents 9166c6c + 5ecc010 commit df0fb2b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
18 changes: 18 additions & 0 deletions server/migrations/1650636333167-UserSavingsRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class UserSavingsRecord1650636333167 implements MigrationInterface {
name = 'UserSavingsRecord1650636333167'

public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
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"`);
}

}
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -75,7 +76,8 @@ import { UpdateLeaveRequestCommandHandler } from 'src/Application/HumanResource/
Leave,
Event,
Cooperative,
MealTicketRemoval
MealTicketRemoval,
UserSavingsRecord
])
],
controllers: [
Expand Down

0 comments on commit df0fb2b

Please sign in to comment.