Skip to content

Commit

Permalink
refactor: add Entity suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
Mendes Hugo committed Sep 11, 2023
1 parent a6dae10 commit d51061f
Show file tree
Hide file tree
Showing 55 changed files with 249 additions and 229 deletions.
4 changes: 2 additions & 2 deletions apps/backend/src/app/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { authOptions } from "~/lib/common/options";
import { UseAuth } from "./auth.guard";
import { AuthLocalGuard } from "./auth.local-guard";
import { AuthService } from "./auth.service";
import { User as UserEntity, User } from "../user/user.entity";
import { UserEntity } from "../user/user.entity";

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace -- TODO: set it as a custom global type AND still working for e2e tests
Expand Down Expand Up @@ -95,7 +95,7 @@ export class AuthController implements AuthEndpoint {
return this.loginOrRefresh(req!.user!, body, res!);
}

private loginOrRefresh(user: User, body: AuthRefreshDto, res: Response) {
private loginOrRefresh(user: UserEntity, body: AuthRefreshDto, res: Response) {
return this.service.login(user).then(token => {
if (body.cookie) {
res.cookie(authOptions.cookies.name, token.access_token, {
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/app/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AuthSuccessDto } from "~/lib/common/app/auth/dtos/auth.success.dto";

import { JWTPayload } from "./auth.types";
import { getConfiguration } from "../../configuration";
import { User } from "../user/user.entity";
import { UserEntity } from "../user/user.entity";
import { UserService } from "../user/user.service";

/**
Expand Down Expand Up @@ -52,7 +52,7 @@ export class AuthService {
* @param user The user to log
* @returns The success message with the access_token
*/
public async login(user: User): Promise<AuthSuccessDto> {
public async login(user: UserEntity): Promise<AuthSuccessDto> {
const access_token = await this.jwtService.signAsync({
_id: user._id,
email: user.email,
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/app/auth/auth.types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { User } from "../user/user.entity";
import { UserEntity } from "../user/user.entity";

/**
* Payload inside a JWT
*/
export interface JWTPayload extends Pick<User, "_id" | "email"> {
export interface JWTPayload extends Pick<UserEntity, "_id" | "email"> {
/**
* How the login has been made (only local here, but could be Google, FB, Microsoft, ...)
*/
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/app/category/category.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "~/lib/common/app/category/dtos";
import { CATEGORIES_ENDPOINT_PREFIX, CategoryEndpoint } from "~/lib/common/app/category/endpoints";

import { Category } from "./category.entity";
import { CategoryEntity } from "./category.entity";
import { CategoryService } from "./category.service";
import { UseAuth } from "../auth/auth.guard";

Expand All @@ -19,7 +19,7 @@ import { UseAuth } from "../auth/auth.guard";
@ApiTags("Categories")
@Controller(CATEGORIES_ENDPOINT_PREFIX)
@UseAuth()
export class CategoryController implements CategoryEndpoint<Category> {
export class CategoryController implements CategoryEndpoint<CategoryEntity> {
/**
* Constructor with "dependency injection"
*
Expand Down
8 changes: 4 additions & 4 deletions apps/backend/src/app/category/category.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { DtoToEntity } from "~/lib/common/dtos/entity/entity.types";

import { CategoryRepository } from "./category.repository";
import { EntityBase } from "../_lib/entity";
import { Node } from "../node/node.entity";
import { NodeEntity } from "../node/node.entity";

/**
* The entity class to manage categories
*/
@Entity({ customRepository: () => CategoryRepository })
export class Category extends EntityBase implements DtoToEntity<CategoryDto> {
export class CategoryEntity extends EntityBase implements DtoToEntity<CategoryDto> {
/**
* @inheritDoc
*/
Expand All @@ -19,6 +19,6 @@ export class Category extends EntityBase implements DtoToEntity<CategoryDto> {

// ------- Relations -------

@ManyToMany(() => Node, node => node.categories)
public readonly nodes? = new Collection<Node>(this);
@ManyToMany(() => NodeEntity, node => node.categories)
public readonly nodes? = new Collection<NodeEntity>(this);
}
4 changes: 2 additions & 2 deletions apps/backend/src/app/category/category.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { MikroOrmModule } from "@mikro-orm/nestjs";
import { Module } from "@nestjs/common";

import { CategoryController } from "./category.controller";
import { Category } from "./category.entity";
import { CategoryEntity } from "./category.entity";
import { CategoryService } from "./category.service";

@Module({
controllers: [CategoryController],
exports: [CategoryService],
imports: [MikroOrmModule.forFeature([Category])],
imports: [MikroOrmModule.forFeature([CategoryEntity])],
providers: [CategoryService]
})
export class CategoryModule {}
6 changes: 3 additions & 3 deletions apps/backend/src/app/category/category.repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EntityRepository } from "@mikro-orm/core";

import { Category } from "./category.entity";
import { CategoryEntity } from "./category.entity";

/**
* The repository to manage [categories]{@link Category}.
* The repository to manage [categories]{@link CategoryEntity}.
*/
export class CategoryRepository extends EntityRepository<Category> {}
export class CategoryRepository extends EntityRepository<CategoryEntity> {}
8 changes: 6 additions & 2 deletions apps/backend/src/app/category/category.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { Injectable } from "@nestjs/common";
import { CategoryCreateDto, CategoryUpdateDto } from "~/lib/common/app/category/dtos";

import { Category } from "./category.entity";
import { CategoryEntity } from "./category.entity";
import { CategoryRepository } from "./category.repository";
import { EntityService } from "../_lib/entity";

/**
* Service to manages [categories]{@link category}.
*/
@Injectable()
export class CategoryService extends EntityService<Category, CategoryCreateDto, CategoryUpdateDto> {
export class CategoryService extends EntityService<
CategoryEntity,
CategoryCreateDto,
CategoryUpdateDto
> {
/**
* Constructor with "dependency injection"
*
Expand Down
20 changes: 10 additions & 10 deletions apps/backend/src/app/graph/arc/graph-arc.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ import { NodeKindType } from "~/lib/common/app/node/dtos/kind";
import { EntityId } from "~/lib/common/dtos/entity";
import { UnshiftParameters } from "~/lib/common/types";

import { GraphArc } from "./graph-arc.entity";
import { GraphArcEntity } from "./graph-arc.entity";
import { GraphArcService } from "./graph-arc.service";
import { UseAuth } from "../../auth/auth.guard";
import { Graph } from "../graph.entity";
import { GraphEntity } from "../graph.entity";
import { ApiGraphParam, GraphInterceptedParam, GraphInterceptor } from "../graph.interceptor";

type EndpointBase = GraphArcEndpoint<GraphArc>;
type EndpointBase = GraphArcEndpoint<GraphArcEntity>;
type EndpointTransformed = {
// Adds a Graph as a first parameter for each function
[K in keyof EndpointBase]: UnshiftParameters<EndpointBase[K], [Graph]>;
[K in keyof EndpointBase]: UnshiftParameters<EndpointBase[K], [GraphEntity]>;
};

@ApiTags("Graph arcs")
Expand All @@ -50,7 +50,7 @@ export class GraphArcController implements EndpointTransformed {
@ApiOkResponse({ type: GraphArcResultsDto })
@Get()
public findAndCount(
@GraphInterceptedParam() graph: Graph,
@GraphInterceptedParam() graph: GraphEntity,
@Query() { where = {}, ...params }: GraphArcQueryDto = {}
) {
return this.service.findAndCount(
Expand All @@ -70,28 +70,28 @@ export class GraphArcController implements EndpointTransformed {
@ApiGraphParam()
@ApiOkResponse({ type: GraphArcDto })
@Get("/:id")
public findById(@GraphInterceptedParam() graph: Graph, @Param("id") id: number) {
public findById(@GraphInterceptedParam() graph: GraphEntity, @Param("id") id: number) {
return this.validateArcId(graph, id);
}

@ApiCreatedResponse({ type: GraphArcDto })
@ApiGraphParam()
@Post()
public create(@GraphInterceptedParam() _graph: Graph, @Body() body: GraphArcCreateDto) {
public create(@GraphInterceptedParam() _graph: GraphEntity, @Body() body: GraphArcCreateDto) {
// TODO: verify graph
return this.service.create(body);
}

@ApiExcludeEndpoint()
@Patch("/:id")
public update(@GraphInterceptedParam() _: Graph, @Param("id") id: number) {
public update(@GraphInterceptedParam() _: GraphEntity, @Param("id") id: number) {
return this.service.update(id, {});
}

@ApiGraphParam()
@ApiOkResponse({ type: GraphArcDto })
@Delete("/:id")
public delete(@GraphInterceptedParam() graph: Graph, @Param("id") id: number) {
public delete(@GraphInterceptedParam() graph: GraphEntity, @Param("id") id: number) {
return this.validateArcId(graph, id).then(({ _id }) => this.service.delete(_id));
}

Expand All @@ -102,7 +102,7 @@ export class GraphArcController implements EndpointTransformed {
* @param id of the arc
* @returns the found arc
*/
private validateArcId(graph: Graph, id: number): Promise<GraphArc> {
private validateArcId(graph: GraphEntity, id: number): Promise<GraphArcEntity> {
return this.service
.findById(id, { populate: { from: { node: true }, to: { node: true } } })
.then(arc => {
Expand Down
14 changes: 7 additions & 7 deletions apps/backend/src/app/graph/arc/graph-arc.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { DtoToEntity } from "~/lib/common/dtos/entity/entity.types";
import { GraphArcRepository } from "./graph-arc.repository";
import { EntityBase } from "../../_lib/entity";
import { ManyToOneFactory, ManyToOneParams } from "../../_lib/entity/decorators";
import { NodeInput } from "../../node/input";
import { NodeOutput } from "../../node/output";
import { NodeInputEntity } from "../../node/input";
import { NodeOutputEntity } from "../../node/output";

/**
* Decorator for the "from" relation
*/
const FromProperty = ManyToOneFactory(() => NodeOutput, {
const FromProperty = ManyToOneFactory(() => NodeOutputEntity, {
fieldName: "__from" satisfies keyof GraphArcDto,
// Deleting a GraphNodeOutput deletes its arcs
onDelete: "cascade",
Expand All @@ -29,7 +29,7 @@ function ToProperty(params: Pick<ManyToOneParams, "foreign">) {
const { foreign } = params;

return applyDecorators(
OneToOne(() => NodeInput, {
OneToOne(() => NodeInputEntity, {
fieldName: "__to" satisfies keyof GraphArcDto,
hidden: foreign,
mapToPk: !foreign,
Expand All @@ -44,7 +44,7 @@ function ToProperty(params: Pick<ManyToOneParams, "foreign">) {
}

@Entity({ customRepository: () => GraphArcRepository })
export class GraphArc extends EntityBase implements DtoToEntity<GraphArcDto> {
export class GraphArcEntity extends EntityBase implements DtoToEntity<GraphArcDto> {
/** @inheritDoc */
@FromProperty({ foreign: false })
public readonly __from!: number;
Expand All @@ -56,8 +56,8 @@ export class GraphArc extends EntityBase implements DtoToEntity<GraphArcDto> {

/** @inheritDoc */
@FromProperty({ foreign: true })
public readonly from?: NodeOutput;
public readonly from?: NodeOutputEntity;
/** @inheritDoc */
@ToProperty({ foreign: true })
public readonly to?: NodeInput;
public readonly to?: NodeInputEntity;
}
6 changes: 3 additions & 3 deletions apps/backend/src/app/graph/arc/graph-arc.repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EntityRepository } from "@mikro-orm/core";

import { GraphArc } from "./graph-arc.entity";
import { GraphArcEntity } from "./graph-arc.entity";

/**
* The repository to manage [graph-arcs]{@link GraphArc}.
* The repository to manage [graph-arcs]{@link GraphArcEntity}.
*/
export class GraphArcRepository extends EntityRepository<GraphArc> {}
export class GraphArcRepository extends EntityRepository<GraphArcEntity> {}
14 changes: 7 additions & 7 deletions apps/backend/src/app/graph/arc/graph-arc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ import { NodeKindType } from "~/lib/common/app/node/dtos/kind";
import { EntityId } from "~/lib/common/dtos/entity";

import { GraphArcDifferentGraphException } from "./exceptions";
import { GraphArc } from "./graph-arc.entity";
import { GraphArcEntity } from "./graph-arc.entity";
import { GraphArcRepository } from "./graph-arc.repository";
import { EntityService } from "../../_lib/entity";
import { NodeService } from "../../node/node.service";
import { GraphCyclicException } from "../exceptions";

/**
* Service to manages [graph-arcs]{@link GraphArc}.
* Service to manages [graph-arcs]{@link GraphArcEntity}.
*/
@Injectable()
export class GraphArcService
extends EntityService<GraphArc, GraphArcCreateDto, Record<string, never>>
implements EventSubscriber<GraphArc>
extends EntityService<GraphArcEntity, GraphArcCreateDto, Record<string, never>>
implements EventSubscriber<GraphArcEntity>
{
/**
* Constructor with "dependency injection"
Expand All @@ -46,14 +46,14 @@ export class GraphArcService
/**
* @inheritDoc
*/
public getSubscribedEntities(): Array<EntityName<GraphArc>> {
return [GraphArc];
public getSubscribedEntities(): Array<EntityName<GraphArcEntity>> {
return [GraphArcEntity];
}

/**
* @inheritDoc
*/
public async beforeCreate(event: EventArgs<GraphArc>) {
public async beforeCreate(event: EventArgs<GraphArcEntity>) {
const {
entity: { __from, __to }
} = event;
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/app/graph/graph.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
GRAPHS_ENDPOINT_PREFIX
} from "~/lib/common/app/graph/endpoints/graph.endpoint";

import { Graph } from "./graph.entity";
import { GraphEntity } from "./graph.entity";
import { GraphService } from "./graph.service";
import { UseAuth } from "../auth/auth.guard";

Expand All @@ -16,7 +16,7 @@ import { UseAuth } from "../auth/auth.guard";
@ApiTags("Graphs")
@Controller(GRAPHS_ENDPOINT_PREFIX)
@UseAuth()
export class GraphController implements GraphEndpoint<Graph> {
export class GraphController implements GraphEndpoint<GraphEntity> {
/**
* Constructor with "dependency injection"
*
Expand Down
8 changes: 4 additions & 4 deletions apps/backend/src/app/graph/graph.entities.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GraphArc } from "./arc/graph-arc.entity";
import { Graph } from "./graph.entity";
import { GraphArcEntity } from "./arc/graph-arc.entity";
import { GraphEntity } from "./graph.entity";

/**
* All entities to manage {@link Graph}
* All entities to manage {@link GraphEntity}
*/
export const GRAPH_ENTITIES = [Graph, GraphArc];
export const GRAPH_ENTITIES = [GraphEntity, GraphArcEntity];
8 changes: 4 additions & 4 deletions apps/backend/src/app/graph/graph.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { DtoToEntity } from "~/lib/common/dtos/entity/entity.types";
import { GraphRepository } from "./graph.repository";
import { EntityBase } from "../_lib/entity";
import { NodeBehaviorFunction } from "../node/behaviors/node-behavior.function";
import { Workflow } from "../workflow/workflow.entity";
import { WorkflowEntity } from "../workflow/workflow.entity";

@Entity({ customRepository: () => GraphRepository })
export class Graph extends EntityBase implements DtoToEntity<GraphDto> {
export class GraphEntity extends EntityBase implements DtoToEntity<GraphDto> {
// ------- Relations -------

/**
Expand All @@ -18,6 +18,6 @@ export class Graph extends EntityBase implements DtoToEntity<GraphDto> {
@OneToOne(() => NodeBehaviorFunction, ({ graph }) => graph, { hidden: true, owner: false })
public readonly nodeBehavior?: NodeBehaviorFunction | null;

@OneToOne(() => Workflow, ({ graph }) => graph, { hidden: true, owner: false })
public readonly workflow?: Workflow | null;
@OneToOne(() => WorkflowEntity, ({ graph }) => graph, { hidden: true, owner: false })
public readonly workflow?: WorkflowEntity | null;
}
6 changes: 3 additions & 3 deletions apps/backend/src/app/graph/graph.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { ParameterObject } from "@nestjs/swagger/dist/interfaces/open-api-spec.i
import { isInt } from "class-validator";
import { Request } from "express";

import { Graph } from "./graph.entity";
import { GraphEntity } from "./graph.entity";
import { GraphService } from "./graph.service";

// eslint-disable-next-line no-use-before-define -- Only for typing
type RequestParams = Partial<Record<typeof GraphInterceptor.GRAPH_TOKEN, Graph>> &
type RequestParams = Partial<Record<typeof GraphInterceptor.GRAPH_TOKEN, GraphEntity>> &
// eslint-disable-next-line no-use-before-define -- Only for typing
Record<typeof GraphInterceptor.PATH_PARAM, string>;

Expand All @@ -34,7 +34,7 @@ export class GraphInterceptor implements NestInterceptor {
*/
public static readonly PATH_PARAM = "graphId";
/**
* The "token" it uses to share the found {@link Graph}
* The "token" it uses to share the found {@link GraphEntity}
*/
public static readonly GRAPH_TOKEN = `__graph__`;

Expand Down
6 changes: 3 additions & 3 deletions apps/backend/src/app/graph/graph.repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EntityRepository } from "@mikro-orm/core";

import { Graph } from "./graph.entity";
import { GraphEntity } from "./graph.entity";

/**
* The repository to manage [graphs]{@link Graph}.
* The repository to manage [graphs]{@link GraphEntity}.
*/
export class GraphRepository extends EntityRepository<Graph> {}
export class GraphRepository extends EntityRepository<GraphEntity> {}
Loading

0 comments on commit d51061f

Please sign in to comment.