Skip to content

Commit

Permalink
create region complete flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Janderson Souza Matias authored and Janderson Souza Matias committed Dec 4, 2023
1 parent 334f87d commit 23ac3ca
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 56 deletions.
1 change: 1 addition & 0 deletions .github/workflows/nepal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- nepal

jobs:
build:
Expand Down
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: "3.8"

services:
postgres_db:
image: postgres:latest
container_name: postgres
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres123
- POSTGRES_DB=coachnpdb
ports:
- "5432:5432"
volumes:
- ./database:/var/lib/postgresql/data
20 changes: 20 additions & 0 deletions src/database/migrations/1701434781415-migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class migrations1701434781415 implements MigrationInterface {
name = 'migrations1701434781415'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "region" ADD "parent_id" uuid`);
await queryRunner.query(`ALTER TABLE "region" ALTER COLUMN "updated_at" SET NOT NULL`);
await queryRunner.query(`ALTER TABLE "region" ALTER COLUMN "updated_at" SET DEFAULT now()`);
await queryRunner.query(`ALTER TABLE "region" ADD CONSTRAINT "FK_2547f55866c0274d35fdd9a29c0" FOREIGN KEY ("parent_id") REFERENCES "region"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "region" DROP CONSTRAINT "FK_2547f55866c0274d35fdd9a29c0"`);
await queryRunner.query(`ALTER TABLE "region" ALTER COLUMN "updated_at" DROP DEFAULT`);
await queryRunner.query(`ALTER TABLE "region" ALTER COLUMN "updated_at" DROP NOT NULL`);
await queryRunner.query(`ALTER TABLE "region" DROP COLUMN "parent_id"`);
}

}
18 changes: 18 additions & 0 deletions src/database/migrations/1701624768823-migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class migrations1701624768823 implements MigrationInterface {
name = 'migrations1701624768823'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "region" DROP CONSTRAINT "FK_2547f55866c0274d35fdd9a29c0"`);
await queryRunner.query(`ALTER TABLE "region" ADD "level" integer`);
await queryRunner.query(`ALTER TABLE "region" ADD CONSTRAINT "FK_2547f55866c0274d35fdd9a29c0" FOREIGN KEY ("parent_id") REFERENCES "region"("id") ON DELETE CASCADE ON UPDATE CASCADE`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "region" DROP CONSTRAINT "FK_2547f55866c0274d35fdd9a29c0"`);
await queryRunner.query(`ALTER TABLE "region" DROP COLUMN "level"`);
await queryRunner.query(`ALTER TABLE "region" ADD CONSTRAINT "FK_2547f55866c0274d35fdd9a29c0" FOREIGN KEY ("parent_id") REFERENCES "region"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
}

}
3 changes: 1 addition & 2 deletions src/modules/dashboard/controller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { DashboardService } from "../service";

import { constants } from "http2";
import { SchoolService } from "../../school/service";
import { School } from "../../school/entity/school.entity";
import { Region } from "../../school/entity/region.entity";
import { Region } from "../../region/entity/region.entity";

const { HTTP_STATUS_OK, HTTP_STATUS_INTERNAL_SERVER_ERROR } = constants;

Expand Down
34 changes: 33 additions & 1 deletion src/modules/region/controller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class SchoolController {
}
};

public static findAll = async (
public static findAllParents = async (
_req: Request,
res: Response
): Promise<any> => {
Expand All @@ -61,4 +61,36 @@ export default class SchoolController {
});
}
};

public static findAllByParent = async (
req: Request,
res: Response
): Promise<any> => {
try {
const list = await RegionService.findByParentId(req.params.id);
return res.status(HTTP_STATUS_OK).send(list);
} catch (error) {
console.log({ error });
res.status(HTTP_STATUS_INTERNAL_SERVER_ERROR).send({
error: HTTP_STATUS_INTERNAL_SERVER_ERROR,
message: (error as any).message,
});
}
};

public static findById = async (
req: Request,
res: Response
): Promise<any> => {
try {
const item = await RegionService.findById(req.params.id);
return res.status(HTTP_STATUS_OK).send(item);
} catch (error) {
console.log({ error });
res.status(HTTP_STATUS_INTERNAL_SERVER_ERROR).send({
error: HTTP_STATUS_INTERNAL_SERVER_ERROR,
message: (error as any).message,
});
}
};
}
26 changes: 25 additions & 1 deletion src/modules/region/entity/region.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
UpdateDateColumn,
Expand All @@ -19,7 +21,27 @@ export class Region {
@Column({ nullable: true })
name?: string;

schoolsCount?: number;
@Column({ nullable: true })
level?: number;

@Column({ nullable: true })
parent_id?: string;

@ManyToOne(() => Region, (region) => region.children, {
onDelete: "CASCADE",
onUpdate: "CASCADE",
orphanedRowAction: "delete",
})
@JoinColumn({ name: "parent_id" })
parent?: Region;

@OneToMany(() => Region, (region) => region.parent, {
cascade: true,
onDelete: "CASCADE",
onUpdate: "CASCADE",
orphanedRowAction: "delete",
})
children?: Region[];

@OneToMany(() => School, (school) => school.region)
schools?: School[];
Expand All @@ -30,4 +52,6 @@ export class Region {

@Column({ nullable: true })
deleted_at?: Date;

schoolsCount?: number;
}
17 changes: 16 additions & 1 deletion src/modules/region/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,22 @@ const schoolRouter = (app: Application): void => {
app.get(
`/${config.country}/api/region`,
Authentication.authenticate,
RegionController.findAll
RegionController.findAllParents
);
app.get(
`/${config.country}/api/region/parent/:id`,
Authentication.authenticate,
RegionController.findAllByParent
);
app.get(
`/${config.country}/api/region/parent`,
Authentication.authenticate,
RegionController.findAllByParent
);
app.get(
`/${config.country}/api/region/:id`,
Authentication.authenticate,
RegionController.findById
);
};

Expand Down
54 changes: 42 additions & 12 deletions src/modules/region/service/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { DeleteResult, UpdateResult } from "typeorm";
import { DeleteResult, IsNull, UpdateResult } from "typeorm";
import dataSource from "../../../database/config/ormconfig";
import { Region } from "../entity/region.entity";
import { School } from "../../school/entity/school.entity";

export class RegionService {
static create = async (data: Region): Promise<Region> => {
Expand All @@ -12,26 +11,57 @@ export class RegionService {

static update = async (
id: string,
{ name }: Region
): Promise<UpdateResult> => {
{ name, children }: Region
): Promise<any | null> => {
const regionRepository = await dataSource.getRepository(Region);

return regionRepository.update(id, { name });
return regionRepository.save({ id, name, children });
};

static delete = async (id: string): Promise<DeleteResult> => {
const schoolRepository = await dataSource.getRepository(Region);
const regionRepository = await dataSource.getRepository(Region);

return regionRepository.delete(id);
};

static findById = async (id: string): Promise<Region | null> => {
const regionRepository = await dataSource.getRepository(Region);

return regionRepository.findOne({
where: { id },
relations: { parent: true, children: { children: true } },
order: {
name: "ASC",
children: { name: "ASC", children: { name: "ASC" } },
},
});
};

static findByParentId = async (parent_id?: string): Promise<Region[]> => {
const regionRepository = await dataSource.getRepository(Region);

return schoolRepository.delete(id);
if (parent_id) {
return regionRepository.find({
where: { parent_id },
relations: { children: true },
order: { name: "ASC" },
});
} else {
return regionRepository.find({
where: { parent_id: IsNull() },
relations: { children: true },
order: { name: "ASC" },
});
}
};

static findAll = async (): Promise<Region[]> => {
const regionRepository = await dataSource.getRepository(Region);

return regionRepository
.createQueryBuilder("region")
.leftJoinAndSelect(School, "school", "school.region_id = region.id")
.loadRelationCountAndMap("region.schoolsCount", "region.schools")
.getMany();
return regionRepository.find({
where: { parent_id: IsNull() },
relations: { children: true },
order: { name: "ASC" },
});
};
}
24 changes: 0 additions & 24 deletions src/modules/school/entity/region.entity.ts

This file was deleted.

13 changes: 1 addition & 12 deletions src/modules/school/entity/school.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "typeorm";
import { CoachSchool } from "../../coach/entity/coach-school.entity";
import { Teacher } from "../../teacher/entity/teacher.entity";
import { Region } from "./region.entity";
import { Region } from "../../region/entity/region.entity";

@Entity()
export class School {
Expand All @@ -29,17 +29,6 @@ export class School {
@Column({ nullable: true })
name?: string;

@Column({ nullable: true })
region_old?:
| "EASTERN"
| "NORTHERN"
| "NORTH WESTERN"
| "WESTERN"
| "SOUTHERN";

@Column({ nullable: true })
district?: string;

@Column({ nullable: true })
emis_number?: string;

Expand Down
9 changes: 6 additions & 3 deletions src/modules/school/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import dataSource from "../../../database/config/ormconfig";
import { School } from "../entity/school.entity";
import crypto from "crypto";
import config from "../../../config";
import { Region } from "../entity/region.entity";
import { Region } from "../../region/entity/region.entity";

const algorithm = "aes-256-ecb";

Expand All @@ -14,10 +14,13 @@ export class SchoolService {
return schoolRepository.save(data);
};

static update = async (id: string, data: School): Promise<UpdateResult> => {
static update = async (
id: string,
{ name, emis_number, region_id }: School
): Promise<School> => {
const schoolRepository = await dataSource.getRepository(School);

return schoolRepository.update(id, data);
return schoolRepository.save({ id, region_id, emis_number, name });
};

static delete = async (id: string): Promise<DeleteResult> => {
Expand Down

0 comments on commit 23ac3ca

Please sign in to comment.