-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
340 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { PhaseHistory } from '../entities/phaseHistory'; | ||
import { Village, VillagePhase } from '../entities/village'; | ||
import { AppDataSource } from '../utils/data-source'; | ||
import { Controller } from './controller'; | ||
|
||
const handlePhase1toPhase3Case = async (village: Village, phase: number) => { | ||
if (village.activePhase === VillagePhase.DISCOVER && phase === VillagePhase.IMAGINE) { | ||
const phaseHistory = new PhaseHistory(); | ||
phaseHistory.village = village; | ||
phaseHistory.phase = VillagePhase.EXCHANGE; | ||
phaseHistory.endingOn = new Date(); | ||
|
||
const phaseHistoryRepository = AppDataSource.getRepository(PhaseHistory); | ||
|
||
await phaseHistoryRepository.save(phaseHistory); | ||
|
||
const endPhase1Query = phaseHistoryRepository | ||
.createQueryBuilder() | ||
.softDelete() | ||
.where('villageId = :villageId', { villageId: village.id }) | ||
.andWhere('phase = :phase', { phase: VillagePhase.DISCOVER }); | ||
await endPhase1Query.execute(); | ||
} | ||
}; | ||
export const phaseHistoryController = new Controller('/phase-history'); | ||
|
||
phaseHistoryController.post({ path: '/' }, async (_req, res) => { | ||
const data = _req.body; | ||
try { | ||
//Find village wit a given id | ||
const villageRepository = AppDataSource.getRepository(Village); | ||
const village = await villageRepository.findOne({ where: { id: data.villageId } }); | ||
if (!village) throw new Error('Village Not found'); | ||
|
||
await handlePhase1toPhase3Case(village, data.phase); | ||
|
||
// Create a PhaseHistory instance and fill it with data | ||
const phaseHistory = new PhaseHistory(); | ||
phaseHistory.village = village; | ||
phaseHistory.phase = data.phase; | ||
phaseHistory.startingOn = new Date(); | ||
|
||
// Save phase history in db | ||
const phaseHistoryRepository = AppDataSource.getRepository(PhaseHistory); | ||
await phaseHistoryRepository.save(phaseHistory); | ||
res.sendStatus(200); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
|
||
phaseHistoryController.delete({ path: '/soft-delete/:villageId/:phase' }, async (_req, res) => { | ||
const { villageId, phase } = _req.params; | ||
const phaseHistoryRepository = AppDataSource.getRepository(PhaseHistory); | ||
const query = phaseHistoryRepository | ||
.createQueryBuilder() | ||
.softDelete() | ||
.where('villageId = :villageId', { villageId }) | ||
.andWhere('phase = :phase', { phase }); | ||
|
||
try { | ||
await query.execute(); | ||
res.sendStatus(202); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); |
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,25 @@ | ||
import { Column, CreateDateColumn, DeleteDateColumn, Entity, Index, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'; | ||
|
||
import type { VillagePhase } from './village'; | ||
import { Village } from './village'; | ||
|
||
@Entity() | ||
@Index('IDX_PHASE_HISTORY', ['village', 'phase'], { unique: true }) | ||
export class PhaseHistory { | ||
@PrimaryGeneratedColumn() | ||
public id: number; | ||
|
||
@ManyToOne(() => Village, (village) => village.phaseHistories, { onDelete: 'CASCADE' }) | ||
village: Village; | ||
|
||
@Column({ | ||
type: 'tinyint', | ||
}) | ||
phase: VillagePhase; | ||
|
||
@CreateDateColumn({ type: 'datetime' }) | ||
public startingOn: Date; | ||
|
||
@DeleteDateColumn({ type: 'datetime' }) | ||
public endingOn: 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,82 @@ | ||
import { Table, TableForeignKey, TableIndex } from 'typeorm'; | ||
import type { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddPhaseHistory1731486908522 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.createTable( | ||
new Table({ | ||
name: 'phase_history', | ||
columns: [ | ||
{ | ||
name: 'id', | ||
type: 'int', | ||
isPrimary: true, | ||
isGenerated: true, | ||
generationStrategy: 'increment', | ||
}, | ||
{ | ||
name: 'villageId', | ||
type: 'int', | ||
isNullable: false, | ||
}, | ||
{ | ||
name: 'phase', | ||
type: 'tinyint', | ||
}, | ||
{ | ||
name: 'startingOn', | ||
type: 'datetime', | ||
}, | ||
{ | ||
name: 'endingOn', | ||
type: 'datetime', | ||
isNullable: true, | ||
}, | ||
], | ||
}), | ||
); | ||
|
||
await queryRunner.createIndex( | ||
'phase_history', | ||
new TableIndex({ | ||
name: 'IDX_PHASE_HISTORY', | ||
columnNames: ['villageId', 'phase'], | ||
isUnique: true, | ||
}), | ||
); | ||
|
||
await queryRunner.createForeignKey( | ||
'phase_history', | ||
new TableForeignKey({ | ||
columnNames: ['villageId'], | ||
referencedColumnNames: ['id'], | ||
referencedTableName: 'village', | ||
onDelete: 'CASCADE', | ||
}), | ||
); | ||
|
||
const villages = await queryRunner.query(`SELECT id FROM village`); | ||
const startingOn2024Phase1Date = '2024-09-30 00:00:00.000000'; | ||
|
||
for (const village of villages) { | ||
await queryRunner.query(`INSERT INTO phase_history (villageId, phase, startingOn, endingOn) VALUES (?, ?, ?, ?)`, [ | ||
village.id, | ||
1, | ||
startingOn2024Phase1Date, | ||
null, | ||
]); | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
const table = await queryRunner.getTable('phase_history'); | ||
if (table) { | ||
const foreignKey = table.foreignKeys.find((fk) => fk.columnNames.indexOf('villageId') !== -1); | ||
if (foreignKey) { | ||
await queryRunner.dropForeignKey('phase_history', foreignKey); | ||
await queryRunner.dropIndex('phase_history', 'IDX_PHASE_HISTORY'); | ||
await queryRunner.dropTable('phase_history'); | ||
} | ||
} | ||
} | ||
} |
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,15 @@ | ||
import type { PhaseHistory } from 'server/entities/phaseHistory'; | ||
|
||
import { axiosRequest } from 'src/utils/axiosRequest'; | ||
|
||
export async function softDeletePhaseHistory(villageId: number, phase: number): Promise<PhaseHistory> { | ||
const response = await axiosRequest<PhaseHistory>({ | ||
method: 'DELETE', | ||
url: `/phase-history/soft-delete/${villageId}/${phase}`, | ||
}); | ||
if (response.error) { | ||
throw new Error("Erreur lors de la suppresion de l'historique des phases. Veuillez réessayer."); | ||
} | ||
|
||
return response.data; | ||
} |
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,16 @@ | ||
import type { PhaseHistory } from 'server/entities/phaseHistory'; | ||
|
||
import { axiosRequest } from 'src/utils/axiosRequest'; | ||
|
||
export async function postPhaseHistory(data: Partial<PhaseHistory> & { villageId: number }): Promise<PhaseHistory> { | ||
const response = await axiosRequest<PhaseHistory>({ | ||
method: 'POST', | ||
url: '/phase-history', | ||
data, | ||
}); | ||
if (response.error) { | ||
throw new Error("Erreur lors de la création de l'historique des phases. Veuillez réessayer."); | ||
} | ||
|
||
return response.data; | ||
} |
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.