-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #254 from fairnesscoop/feat/add-user-savings-recor…
…d-model Add UserSavingsRecord model
- Loading branch information
Showing
4 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"`); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
server/src/Domain/HumanResource/Savings/UserSavingsRecord.entity.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
56 changes: 56 additions & 0 deletions
56
server/src/Domain/HumanResource/Savings/UserSavingsRecord.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters