-
Notifications
You must be signed in to change notification settings - Fork 35
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 #257 from fairnesscoop/feat/add-interest-rate
Add interest rate to user savings record
- Loading branch information
Showing
12 changed files
with
164 additions
and
5 deletions.
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
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 {MigrationInterface, QueryRunner} from "typeorm"; | ||
|
||
export class InterestRate1651674281488 implements MigrationInterface { | ||
name = 'InterestRate1651674281488' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`CREATE TABLE "interest_rate" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "rate" integer NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "PK_e0dc77d8cda169497a847de0f8b" PRIMARY KEY ("id")); COMMENT ON COLUMN "interest_rate"."rate" IS 'Stored in base 100'`); | ||
await queryRunner.query(`ALTER TABLE "user_savings_record" ADD "interestRateId" uuid`); | ||
await queryRunner.query(`ALTER TABLE "user_savings_record" ADD CONSTRAINT "FK_b88b218db366c70a2ec33e424a2" FOREIGN KEY ("interestRateId") REFERENCES "interest_rate"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`); | ||
await queryRunner.query(`INSERT INTO "interest_rate" VALUES('9ae76df0-2ae6-40f8-a2e2-fad0371bcfa9', '100', '2022-05-04')`, undefined); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "user_savings_record" DROP CONSTRAINT "FK_b88b218db366c70a2ec33e424a2"`); | ||
await queryRunner.query(`ALTER TABLE "user_savings_record" DROP COLUMN "interestRateId"`); | ||
await queryRunner.query(`DROP TABLE "interest_rate"`); | ||
} | ||
|
||
} |
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
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
7 changes: 7 additions & 0 deletions
7
server/src/Domain/HumanResource/Savings/Exception/InterestRateNotFoundException.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,7 @@ | ||
export class InterestRateNotFoundException extends Error { | ||
constructor() { | ||
super( | ||
'human_resources.savings_records.errors.interest_rate_not_found' | ||
); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
server/src/Domain/HumanResource/Savings/InterestRate.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,10 @@ | ||
import { InterestRate } from './InterestRate.entity'; | ||
|
||
describe('InterestRate.entity', () => { | ||
it('testGetters', () => { | ||
const interestRate = new InterestRate(100); | ||
|
||
expect(interestRate.getId()).toBe(undefined); | ||
expect(interestRate.getRate()).toBe(100); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
server/src/Domain/HumanResource/Savings/InterestRate.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,29 @@ | ||
import { | ||
Entity, | ||
Column, | ||
PrimaryGeneratedColumn, | ||
} from 'typeorm'; | ||
|
||
@Entity() | ||
export class InterestRate { | ||
@PrimaryGeneratedColumn('uuid') | ||
private id: string; | ||
|
||
@Column({ type: 'integer', nullable: false, comment: 'Stored in base 100' }) | ||
private rate: number; | ||
|
||
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' }) | ||
private createdAt: Date; | ||
|
||
constructor(rate: number) { | ||
this.rate = rate; | ||
} | ||
|
||
public getId(): string { | ||
return this.id; | ||
} | ||
|
||
public getRate(): number { | ||
return this.rate; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
server/src/Domain/HumanResource/Savings/Repository/IInterestRateRepository.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,5 @@ | ||
import { InterestRate } from '../InterestRate.entity'; | ||
|
||
export interface IInterestRateRepository { | ||
findLatestInterestRate(): Promise<InterestRate>; | ||
} |
6 changes: 5 additions & 1 deletion
6
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 |
---|---|---|
@@ -1,19 +1,23 @@ | ||
import { mock, instance } from 'ts-mockito'; | ||
import { User } from '../User/User.entity'; | ||
import { InterestRate } from './InterestRate.entity'; | ||
import { SavingsRecordType, UserSavingsRecord } from './UserSavingsRecord.entity'; | ||
|
||
describe('UserSavingsRecord.entity', () => { | ||
it('testGetters', () => { | ||
const user = mock(User); | ||
const interestRate = mock(InterestRate); | ||
const userSavingsRecord = new UserSavingsRecord( | ||
100000, | ||
SavingsRecordType.INPUT, | ||
instance(user) | ||
instance(user), | ||
instance(interestRate), | ||
); | ||
|
||
expect(userSavingsRecord.getId()).toBe(undefined); | ||
expect(userSavingsRecord.getAmount()).toBe(100000); | ||
expect(userSavingsRecord.getUser()).toBe(instance(user)); | ||
expect(userSavingsRecord.getInterestRate()).toBe(instance(interestRate)); | ||
expect(userSavingsRecord.getType()).toBe(SavingsRecordType.INPUT); | ||
}); | ||
}); |
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
20 changes: 20 additions & 0 deletions
20
server/src/Infrastructure/HumanResource/Savings/Repository/InterestRateRepository.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,20 @@ | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { IInterestRateRepository } from 'src/Domain/HumanResource/Savings/Repository/IInterestRateRepository'; | ||
import { InterestRate } from 'src/Domain/HumanResource/Savings/InterestRate.entity'; | ||
|
||
export class InterestRateRepository implements IInterestRateRepository { | ||
constructor( | ||
@InjectRepository(InterestRate) | ||
private readonly repository: Repository<InterestRate> | ||
) {} | ||
|
||
public findLatestInterestRate(): Promise<InterestRate> { | ||
return this.repository | ||
.createQueryBuilder('interestRate') | ||
.select(['interestRate.id']) | ||
.limit(1) | ||
.orderBy('interestRate.createdAt', 'DESC') | ||
.getOne(); | ||
} | ||
} |
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