|
| 1 | +import { TPrismaTx } from '../domain/entities'; |
| 2 | +import { |
| 3 | + DirectedGraphHierarchy, |
| 4 | + TEdge, |
| 5 | + TPath, |
| 6 | +} from '../domain/entities/graph.entity'; |
| 7 | +import { ExtendedBadRequestException } from '../frameworks/shared/exceptions/common.exception'; |
| 8 | +import { CyclicException } from '../frameworks/shared/exceptions/graph.exception'; |
| 9 | +import { camelize } from '../frameworks/shared/utils/string.util'; |
| 10 | +import { GraphHelper } from './helpers'; |
| 11 | + |
| 12 | +export class BaseGraphRepository< |
| 13 | + Entity extends Record<string, any>, |
| 14 | + Include extends Record<string, any>, |
| 15 | + Where extends Record<string, any>, |
| 16 | +> { |
| 17 | + protected _entity: string; |
| 18 | + protected _tableName: string; |
| 19 | + |
| 20 | + public defaultInclude: Include; |
| 21 | + public defaultWhere: Where; |
| 22 | + |
| 23 | + constructor(entity: new () => Entity) { |
| 24 | + this._entity = camelize(entity.name.substring(5)); |
| 25 | + this._tableName = `_${entity.name.substring(5)}Groups`; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * ## buildGraph |
| 30 | + * This function builds the graph hierarchy of this entity which are defined from generic **Entity**. |
| 31 | + * there's no filter entities which will be generate a paths then into hierarchy. |
| 32 | + * **Carefully to used this function it may affect performance!. Because generate all path it same as looping through all data.** |
| 33 | + * @param tx TPrismaTx |
| 34 | + * @param include Include |
| 35 | + * @returns Promise<DirectedGraphHierarchy<Entity>> |
| 36 | + */ |
| 37 | + public async buildGraph( |
| 38 | + tx: TPrismaTx, |
| 39 | + include?: Include, |
| 40 | + ): Promise<DirectedGraphHierarchy<Entity>> { |
| 41 | + const graph = new DirectedGraphHierarchy<Entity>((n: Entity) => n.id); |
| 42 | + |
| 43 | + const edges: TEdge[] = await GraphHelper.findEdges(tx, this._tableName); |
| 44 | + |
| 45 | + const nodes: Entity[] = await tx[this._entity].findMany({ |
| 46 | + include, |
| 47 | + }); |
| 48 | + |
| 49 | + nodes.forEach((node) => { |
| 50 | + graph.insert(node); |
| 51 | + }); |
| 52 | + |
| 53 | + edges.forEach((edge) => { |
| 54 | + graph.addEdge(edge.A, edge.B); |
| 55 | + }); |
| 56 | + |
| 57 | + return graph; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * ## buildGraphHierarchyFromId |
| 62 | + * This function builds the graph hierarchy of this entity which are defined from generic **Entity**. |
| 63 | + * fromId is required to filter only specific path that we want to generate paths then into hierarchy |
| 64 | + * @param tx TPrismaTx |
| 65 | + * @param fromId string |
| 66 | + * @param include Include |
| 67 | + * @returns Promise<DirectedGraphHierarchy<Entity>> |
| 68 | + */ |
| 69 | + public async buildGraphHierarchyFromId( |
| 70 | + tx: TPrismaTx, |
| 71 | + fromId: string, |
| 72 | + include?: Include, |
| 73 | + ): Promise<DirectedGraphHierarchy<Entity>> { |
| 74 | + const graph = await this.buildGraph(tx, include); |
| 75 | + |
| 76 | + const path: TPath[] = await GraphHelper.findPathByFromId( |
| 77 | + tx, |
| 78 | + this._tableName, |
| 79 | + fromId, |
| 80 | + ); |
| 81 | + |
| 82 | + const collectionOfNodeIdentity = path.map((path) => path.path); |
| 83 | + graph.createHierarchy(collectionOfNodeIdentity); |
| 84 | + return graph; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * ## createNewBodyForCreate<T> |
| 89 | + * We need to transform body request into prisma format |
| 90 | + * @param body any |
| 91 | + * @param groupsFieldName string |
| 92 | + * @param entriesFieldName string |
| 93 | + * @returns T |
| 94 | + */ |
| 95 | + public createNewBodyForCreate<T>( |
| 96 | + body: any, |
| 97 | + groupsFieldName: string, |
| 98 | + entriesFieldName: string, |
| 99 | + ): T { |
| 100 | + body[groupsFieldName] = { |
| 101 | + connect: body[groupsFieldName].map((groupId: string) => ({ |
| 102 | + id: groupId, |
| 103 | + })), |
| 104 | + }; |
| 105 | + |
| 106 | + body[entriesFieldName] = { |
| 107 | + createMany: { |
| 108 | + data: body[entriesFieldName].map((roleId: string) => ({ |
| 109 | + roleId: roleId, |
| 110 | + })), |
| 111 | + }, |
| 112 | + }; |
| 113 | + |
| 114 | + return body; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * ## createNewBodyForUpdate<T> |
| 119 | + * We need to transform body request into prisma format |
| 120 | + * @param tx TPrismaTx |
| 121 | + * @param body any |
| 122 | + * @param id string |
| 123 | + * @param groupsFieldName string |
| 124 | + * @param entriesFieldName string |
| 125 | + * @returns Promise<T> |
| 126 | + */ |
| 127 | + public async createNewBodyForUpdate<T>( |
| 128 | + tx: TPrismaTx, |
| 129 | + body: any, |
| 130 | + id: string, |
| 131 | + groupsFieldName: string, |
| 132 | + entriesFieldName: string, |
| 133 | + include?: Include, |
| 134 | + ): Promise<T> { |
| 135 | + // Build the graph first |
| 136 | + const graph = await this.buildGraphHierarchyFromId(tx, id, include); |
| 137 | + |
| 138 | + if (body[groupsFieldName]) { |
| 139 | + /** |
| 140 | + * Check and return the existing groups |
| 141 | + */ |
| 142 | + const existingGroups = this.checkCyclicForUpdateOperation( |
| 143 | + id, |
| 144 | + body[groupsFieldName], |
| 145 | + graph, |
| 146 | + ); |
| 147 | + |
| 148 | + body[groupsFieldName] = { |
| 149 | + // Remove all existing groups |
| 150 | + disconnect: existingGroups.map((groupId) => ({ |
| 151 | + id: groupId, |
| 152 | + })), |
| 153 | + // Connect a new groups |
| 154 | + connect: body[groupsFieldName].map((groupId) => ({ |
| 155 | + id: groupId, |
| 156 | + })), |
| 157 | + }; |
| 158 | + } |
| 159 | + |
| 160 | + if (body[entriesFieldName]) { |
| 161 | + body[entriesFieldName] = { |
| 162 | + deleteMany: {}, |
| 163 | + createMany: { |
| 164 | + data: body[entriesFieldName].map((roleId) => ({ |
| 165 | + roleId: roleId, |
| 166 | + })), |
| 167 | + }, |
| 168 | + }; |
| 169 | + } |
| 170 | + |
| 171 | + return body; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * ## checkCyclicForUpdateOperation |
| 176 | + * This method is to check if the candidate groups will contain cyclic or not. |
| 177 | + * @param id string |
| 178 | + * @param groupIds string[] | undefined |
| 179 | + * @param graph DirectedGraphHierarchy<Entity> |
| 180 | + * @returns string[] |
| 181 | + */ |
| 182 | + public checkCyclicForUpdateOperation( |
| 183 | + id: string, |
| 184 | + groupIds: string[] | undefined, |
| 185 | + graph: DirectedGraphHierarchy<Entity>, |
| 186 | + ): string[] { |
| 187 | + const parentNode = graph.getNode(id); |
| 188 | + |
| 189 | + if (!parentNode) { |
| 190 | + throw new ExtendedBadRequestException({ |
| 191 | + message: `id with ${id} does not exist!`, |
| 192 | + }); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Subtract parentGroupIds - body.groupIds. |
| 197 | + * Purpose is to create candidate id |
| 198 | + */ |
| 199 | + const substractGroupIds = groupIds?.filter( |
| 200 | + (value) => !parentNode.groups.map((group) => group.id).includes(value), |
| 201 | + ); |
| 202 | + |
| 203 | + /** |
| 204 | + * Add the list of groupsId that it will be groups or children. |
| 205 | + * We need to put candidate groups into the edge, so we can check whether this id contain cyclic! |
| 206 | + */ |
| 207 | + substractGroupIds?.forEach((groupId) => { |
| 208 | + graph.addEdge(id, groupId); |
| 209 | + }); |
| 210 | + |
| 211 | + /** |
| 212 | + * Check whether is data cyclic!. because cyclic can cause circular references! |
| 213 | + */ |
| 214 | + if (!graph.isAcyclic()) { |
| 215 | + throw new CyclicException({ |
| 216 | + message: 'the candidate groups contain cyclic!', |
| 217 | + }); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Return existing groups |
| 222 | + */ |
| 223 | + return parentNode.groups.map((group) => group.id); |
| 224 | + } |
| 225 | +} |
0 commit comments