forked from Giveth/impact-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #37 from GeneralMagicio/addEarlyAccessRound
Add early access rounds
- Loading branch information
Showing
16 changed files
with
413 additions
and
24 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,41 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddEarlyAccessRoundTable1724799772891 | ||
implements MigrationInterface | ||
{ | ||
name = 'AddEarlyAccessRoundTable1724799772891'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "donation" RENAME COLUMN "earlyAccessRound" TO "earlyAccessRoundId"`, | ||
); | ||
await queryRunner.query( | ||
`CREATE TABLE "early_access_round" ("id" SERIAL NOT NULL, "roundNumber" integer NOT NULL, "startDate" TIMESTAMP NOT NULL, "endDate" TIMESTAMP NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "UQ_e2f9598b0bbed3f05ca5c49fedc" UNIQUE ("roundNumber"), CONSTRAINT "PK_b128520615d2666c576399b07d3" PRIMARY KEY ("id"))`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "donation" DROP COLUMN "earlyAccessRoundId"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "donation" ADD "earlyAccessRoundId" integer`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "donation" ADD CONSTRAINT "FK_635e96839361920b7f80da1dd51" FOREIGN KEY ("earlyAccessRoundId") REFERENCES "early_access_round"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "donation" DROP CONSTRAINT "FK_635e96839361920b7f80da1dd51"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "donation" DROP COLUMN "earlyAccessRoundId"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "donation" ADD "earlyAccessRoundId" boolean DEFAULT false`, | ||
); | ||
await queryRunner.query(`DROP TABLE "early_access_round"`); | ||
await queryRunner.query( | ||
`ALTER TABLE "donation" RENAME COLUMN "earlyAccessRoundId" TO "earlyAccessRound"`, | ||
); | ||
} | ||
} |
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
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,37 @@ | ||
import { | ||
BaseEntity, | ||
Column, | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
CreateDateColumn, | ||
UpdateDateColumn, | ||
} from 'typeorm'; | ||
import { Field, ID, ObjectType, Int } from 'type-graphql'; | ||
|
||
@Entity() | ||
@ObjectType() | ||
export class EarlyAccessRound extends BaseEntity { | ||
@Field(_type => ID) | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Field(() => Int) | ||
@Column({ unique: true }) | ||
roundNumber: number; | ||
|
||
@Field(() => Date) | ||
@Column() | ||
startDate: Date; | ||
|
||
@Field(() => Date) | ||
@Column() | ||
endDate: Date; | ||
|
||
@Field(() => Date) | ||
@CreateDateColumn() | ||
createdAt: Date; | ||
|
||
@Field(() => Date) | ||
@UpdateDateColumn() | ||
updatedAt: Date; | ||
} |
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,93 @@ | ||
import { expect } from 'chai'; | ||
import { EarlyAccessRound } from '../entities/earlyAccessRound'; | ||
import { | ||
findAllEarlyAccessRounds, | ||
findActiveEarlyAccessRound, | ||
} from './earlyAccessRoundRepository'; | ||
import { saveRoundDirectlyToDb } from '../../test/testUtils'; | ||
|
||
describe('EarlyAccessRound Repository Test Cases', () => { | ||
beforeEach(async () => { | ||
// Clean up data before each test case | ||
await EarlyAccessRound.delete({}); | ||
}); | ||
|
||
afterEach(async () => { | ||
// Clean up data after each test case | ||
await EarlyAccessRound.delete({}); | ||
}); | ||
|
||
it('should save a new Early Access Round directly to the database', async () => { | ||
const roundData = { | ||
roundNumber: 1, | ||
startDate: new Date('2024-09-01'), | ||
endDate: new Date('2024-09-05'), | ||
}; | ||
|
||
const savedRound = await saveRoundDirectlyToDb(roundData); | ||
|
||
expect(savedRound).to.be.an.instanceof(EarlyAccessRound); | ||
expect(savedRound.roundNumber).to.equal(roundData.roundNumber); | ||
expect(savedRound.startDate.toISOString()).to.equal( | ||
roundData.startDate.toISOString(), | ||
); | ||
expect(savedRound.endDate.toISOString()).to.equal( | ||
roundData.endDate.toISOString(), | ||
); | ||
}); | ||
|
||
it('should find all Early Access Rounds', async () => { | ||
// Save a couple of rounds first | ||
await saveRoundDirectlyToDb({ | ||
roundNumber: 1, | ||
startDate: new Date('2024-09-01'), | ||
endDate: new Date('2024-09-05'), | ||
}); | ||
await saveRoundDirectlyToDb({ | ||
roundNumber: 2, | ||
startDate: new Date('2024-09-06'), | ||
endDate: new Date('2024-09-10'), | ||
}); | ||
|
||
const rounds = await findAllEarlyAccessRounds(); | ||
|
||
expect(rounds).to.be.an('array'); | ||
expect(rounds.length).to.equal(2); | ||
expect(rounds[0]).to.be.an.instanceof(EarlyAccessRound); | ||
expect(rounds[1]).to.be.an.instanceof(EarlyAccessRound); | ||
}); | ||
|
||
it('should find the active Early Access Round', async () => { | ||
const activeRoundData = { | ||
roundNumber: 1, | ||
startDate: new Date(new Date().setDate(new Date().getDate() - 1)), // yesterday | ||
endDate: new Date(new Date().setDate(new Date().getDate() + 1)), // tomorrow | ||
}; | ||
|
||
const inactiveRoundData = { | ||
roundNumber: 2, | ||
startDate: new Date(new Date().getDate() + 1), | ||
endDate: new Date(new Date().getDate() + 2), | ||
}; | ||
|
||
// Save both active and inactive rounds | ||
await saveRoundDirectlyToDb(activeRoundData); | ||
await saveRoundDirectlyToDb(inactiveRoundData); | ||
|
||
const activeRound = await findActiveEarlyAccessRound(); | ||
|
||
expect(activeRound).to.be.an.instanceof(EarlyAccessRound); | ||
expect(activeRound?.roundNumber).to.equal(activeRoundData.roundNumber); | ||
expect(activeRound?.startDate.toISOString()).to.equal( | ||
activeRoundData.startDate.toISOString(), | ||
); | ||
expect(activeRound?.endDate.toISOString()).to.equal( | ||
activeRoundData.endDate.toISOString(), | ||
); | ||
}); | ||
|
||
it('should return null when no active Early Access Round is found', async () => { | ||
const activeRound = await findActiveEarlyAccessRound(); | ||
expect(activeRound).to.be.null; | ||
}); | ||
}); |
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,32 @@ | ||
import { EarlyAccessRound } from '../entities/earlyAccessRound'; | ||
import { logger } from '../utils/logger'; | ||
|
||
export const findAllEarlyAccessRounds = async (): Promise< | ||
EarlyAccessRound[] | ||
> => { | ||
try { | ||
return EarlyAccessRound.createQueryBuilder('earlyAccessRound') | ||
.orderBy('earlyAccessRound.startDate', 'ASC') | ||
.getMany(); | ||
} catch (error) { | ||
logger.error('Error fetching all Early Access rounds', { error }); | ||
throw new Error('Error fetching Early Access rounds'); | ||
} | ||
}; | ||
|
||
// Find the currently active Early Access Round | ||
export const findActiveEarlyAccessRound = | ||
async (): Promise<EarlyAccessRound | null> => { | ||
const currentDate = new Date(); | ||
|
||
try { | ||
const query = EarlyAccessRound.createQueryBuilder('earlyAccessRound') | ||
.where('earlyAccessRound.startDate <= :currentDate', { currentDate }) | ||
.andWhere('earlyAccessRound.endDate >= :currentDate', { currentDate }); | ||
|
||
return query.getOne(); | ||
} catch (error) { | ||
logger.error('Error fetching active Early Access round', { error }); | ||
throw new Error('Error fetching active Early Access round'); | ||
} | ||
}; |
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
Oops, something went wrong.