#2-protocol #10
10 changed files with 155 additions and 3 deletions
22
src/controller/admin/protocolController.ts
Normal file
22
src/controller/admin/protocolController.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import { Request, Response } from "express";
|
||||
import ProtocolService from "../../service/protocolService";
|
||||
import ProtocolFactory from "../../factory/admin/protocol";
|
||||
|
||||
/**
|
||||
* @description get all protocols
|
||||
* @param req {Request} Express req object
|
||||
* @param res {Response} Express res object
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
export async function getAllProtocols(req: Request, res: Response): Promise<any> {
|
||||
let offset = parseInt((req.query.offset as string) ?? "0");
|
||||
let count = parseInt((req.query.count as string) ?? "25");
|
||||
let [protocols, total] = await ProtocolService.getAll(offset, count);
|
||||
|
||||
res.json({
|
||||
protocols: ProtocolFactory.mapToBase(protocols),
|
||||
total: total,
|
||||
offset: offset,
|
||||
count: count,
|
||||
});
|
||||
}
|
|
@ -29,6 +29,8 @@ import { memberQualifications } from "./entity/memberQualifications";
|
|||
import { membership } from "./entity/membership";
|
||||
import { Memberdata1726301836849 } from "./migrations/1726301836849-memberdata";
|
||||
import { CommunicationFields1727439800630 } from "./migrations/1727439800630-communicationFields";
|
||||
import { protocol } from "./entity/protocol";
|
||||
import { ProtocolInit1727953803404 } from "./migrations/1727953803404-protocol-init";
|
||||
|
||||
const dataSource = new DataSource({
|
||||
type: DB_TYPE as any,
|
||||
|
@ -58,6 +60,7 @@ const dataSource = new DataSource({
|
|||
memberExecutivePositions,
|
||||
memberQualifications,
|
||||
membership,
|
||||
protocol,
|
||||
],
|
||||
migrations: [
|
||||
Initial1724317398939,
|
||||
|
@ -68,6 +71,7 @@ const dataSource = new DataSource({
|
|||
MemberBaseData1725435669492,
|
||||
Memberdata1726301836849,
|
||||
CommunicationFields1727439800630,
|
||||
ProtocolInit1727953803404,
|
||||
],
|
||||
migrationsRun: true,
|
||||
migrationsTransactionMode: "each",
|
||||
|
|
13
src/entity/protocol.ts
Normal file
13
src/entity/protocol.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import { Column, Entity, PrimaryColumn } from "typeorm";
|
||||
|
||||
@Entity()
|
||||
export class protocol {
|
||||
@PrimaryColumn({ generated: "increment", type: "int" })
|
||||
id: number;
|
||||
|
||||
@Column({ type: "varchar", length: 255 })
|
||||
title: string;
|
||||
|
||||
@Column({ type: "date" })
|
||||
date: Date;
|
||||
}
|
26
src/factory/admin/protocol.ts
Normal file
26
src/factory/admin/protocol.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
import { protocol } from "../../entity/protocol";
|
||||
import { ProtocolViewModel } from "../../viewmodel/admin/protocol.models";
|
||||
|
||||
export default abstract class ProtocolFactory {
|
||||
/**
|
||||
* @description map record to protocol
|
||||
* @param {protocol} record
|
||||
* @returns {ProtocolViewModel}
|
||||
*/
|
||||
public static mapToSingle(record: protocol): ProtocolViewModel {
|
||||
return {
|
||||
id: record.id,
|
||||
title: record.title,
|
||||
date: record.date,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description map records to protocol
|
||||
* @param {Array<protocol>} records
|
||||
* @returns {Array<ProtocolViewModel>}
|
||||
*/
|
||||
public static mapToBase(records: Array<protocol>): Array<ProtocolViewModel> {
|
||||
return records.map((r) => this.mapToSingle(r));
|
||||
}
|
||||
}
|
26
src/migrations/1727953803404-protocol-init.ts
Normal file
26
src/migrations/1727953803404-protocol-init.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
import { MigrationInterface, QueryRunner, Table } from "typeorm";
|
||||
import { DB_TYPE } from "../env.defaults";
|
||||
|
||||
export class ProtocolInit1727953803404 implements MigrationInterface {
|
||||
name = "ProtocolInit1727953803404";
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
const variableType_int = DB_TYPE == "mysql" ? "int" : "integer";
|
||||
|
||||
await queryRunner.createTable(
|
||||
new Table({
|
||||
name: "protocol",
|
||||
columns: [
|
||||
{ name: "id", type: variableType_int, isPrimary: true, isGenerated: true, generationStrategy: "increment" },
|
||||
{ name: "title", type: "varchar", length: "255", isNullable: false },
|
||||
{ name: "date", type: "date", isNullable: false },
|
||||
],
|
||||
}),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.dropTable("protocol");
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ import membershipStatus from "./membershipStatus";
|
|||
import qualification from "./qualification";
|
||||
|
||||
import member from "./member";
|
||||
import protocol from "./protocol";
|
||||
|
||||
import role from "./role";
|
||||
import user from "./user";
|
||||
|
@ -34,6 +35,8 @@ router.use("/qualification", PermissionHelper.passCheckMiddleware("read", "setti
|
|||
|
||||
router.use("/member", PermissionHelper.passCheckMiddleware("read", "club", "member"), member);
|
||||
|
||||
router.use("/protocol", PermissionHelper.passCheckMiddleware("read", "club", "protocol"), protocol);
|
||||
|
||||
router.use("/role", PermissionHelper.passCheckMiddleware("read", "user", "role"), role);
|
||||
router.use("/user", PermissionHelper.passCheckMiddleware("read", "user", "user"), user);
|
||||
|
||||
|
|
10
src/routes/admin/protocol.ts
Normal file
10
src/routes/admin/protocol.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
import express, { Request, Response } from "express";
|
||||
import { getAllProtocols } from "../../controller/admin/protocolController";
|
||||
|
||||
var router = express.Router({ mergeParams: true });
|
||||
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
await getAllProtocols(req, res);
|
||||
});
|
||||
|
||||
export default router;
|
43
src/service/protocolService.ts
Normal file
43
src/service/protocolService.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
import { dataSource } from "../data-source";
|
||||
import { protocol } from "../entity/protocol";
|
||||
import InternalException from "../exceptions/internalException";
|
||||
|
||||
export default abstract class ProtocolService {
|
||||
/**
|
||||
* @description get all protocols
|
||||
* @returns {Promise<[Array<protocol>, number]>}
|
||||
*/
|
||||
static async getAll(offset: number = 0, count: number = 25): Promise<[Array<protocol>, number]> {
|
||||
return await dataSource
|
||||
.getRepository(protocol)
|
||||
.createQueryBuilder("protocol")
|
||||
.offset(offset)
|
||||
.limit(count)
|
||||
.orderBy("date")
|
||||
.getManyAndCount()
|
||||
.then((res) => {
|
||||
return res;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new InternalException("protocols not found", err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description get protocol by id
|
||||
* @returns {Promise<protocol>}
|
||||
*/
|
||||
static async getById(id: number): Promise<protocol> {
|
||||
return await dataSource
|
||||
.getRepository(protocol)
|
||||
.createQueryBuilder("protocol")
|
||||
.where("protocol.id = :id", { id: id })
|
||||
.getOneOrFail()
|
||||
.then((res) => {
|
||||
return res;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new InternalException("protocol not found by id", err);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@ export type PermissionModule =
|
|||
| "member"
|
||||
| "calendar"
|
||||
| "newsletter"
|
||||
| "protocoll"
|
||||
| "protocol"
|
||||
| "qualification"
|
||||
| "award"
|
||||
| "executive_position"
|
||||
|
@ -39,7 +39,7 @@ export const permissionModules: Array<PermissionModule> = [
|
|||
"member",
|
||||
"calendar",
|
||||
"newsletter",
|
||||
"protocoll",
|
||||
"protocol",
|
||||
"qualification",
|
||||
"award",
|
||||
"executive_position",
|
||||
|
@ -50,7 +50,7 @@ export const permissionModules: Array<PermissionModule> = [
|
|||
];
|
||||
export const permissionTypes: Array<PermissionType> = ["read", "create", "update", "delete"];
|
||||
export const sectionsAndModules: SectionsAndModulesObject = {
|
||||
club: ["member", "calendar", "newsletter", "protocoll"],
|
||||
club: ["member", "calendar", "newsletter", "protocol"],
|
||||
settings: ["qualification", "award", "executive_position", "communication", "membership_status"],
|
||||
user: ["user", "role"],
|
||||
};
|
||||
|
|
5
src/viewmodel/admin/protocol.models.ts
Normal file
5
src/viewmodel/admin/protocol.models.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
export interface ProtocolViewModel {
|
||||
id: number;
|
||||
title: string;
|
||||
date: Date;
|
||||
}
|
Loading…
Reference in a new issue