From 2c7b550742c526f3255b7c96d4ffcd34e3ca74f5 Mon Sep 17 00:00:00 2001 From: Kasshern Date: Mon, 13 Nov 2023 11:58:15 -0600 Subject: [PATCH 01/10] Moves prisma instatiation into own module, imports that module into storage agents, deletes storage agent extension of prismaService. --- examples/bri-3/src/app.module.ts | 2 +- .../bri-3/src/bri/authz/guards/authz.guard.ts | 2 +- .../agents/bpiMessagesStorage.agent.ts | 19 +++-- .../bri/communication/communication.module.ts | 2 + .../identity/bpiAccounts/accounts.module.ts | 3 +- .../agents/bpiAccountsStorage.agent.ts | 30 +++---- .../agents/bpiSubjectAccountsStorage.agent.ts | 80 ++++++++++--------- .../subjectAccounts.module.ts | 3 +- .../agents/bpiSubjectsStorage.agent.ts | 27 ++++--- .../identity/bpiSubjects/subjects.module.ts | 3 +- .../import MerkleTree from 'merkletreejs';.ts | 20 ----- .../agents/merkleTreeStorage.agent.ts | 19 ++--- .../bri-3/src/bri/merkleTree/merkle.module.ts | 9 ++- .../agents/transactionStorage.agent.ts | 25 +++--- .../bri/transactions/transactions.module.ts | 2 + .../agents/workflowsStorage.agent.ts | 23 +++--- .../workgroup/workflows/workflows.module.ts | 2 + .../agents/workgroupStorage.agent.ts | 19 ++--- .../workgroup/workgroups/workgroups.module.ts | 3 +- .../agents/workstepsStorage.agent.ts | 25 +++--- .../workgroup/worksteps/worksteps.module.ts | 3 +- .../bri-3/src/shared/prisma/prisma.module.ts | 8 ++ .../{ => src/shared}/prisma/prisma.service.ts | 3 + 23 files changed, 177 insertions(+), 155 deletions(-) delete mode 100644 examples/bri-3/src/bri/merkleTree/agents/import MerkleTree from 'merkletreejs';.ts create mode 100644 examples/bri-3/src/shared/prisma/prisma.module.ts rename examples/bri-3/{ => src/shared}/prisma/prisma.service.ts (82%) diff --git a/examples/bri-3/src/app.module.ts b/examples/bri-3/src/app.module.ts index 1e3e425da..b3b4e5cc0 100644 --- a/examples/bri-3/src/app.module.ts +++ b/examples/bri-3/src/app.module.ts @@ -2,7 +2,6 @@ import { classes } from '@automapper/classes'; import { AutomapperModule } from '@automapper/nestjs'; import { Module } from '@nestjs/common'; import { APP_GUARD } from '@nestjs/core'; -import { PrismaService } from '../prisma/prisma.service'; import { AuthModule } from './bri/auth/auth.module'; import { DidJwtAuthGuard } from './bri/auth/guards/didJwt.guard'; import { AuthzModule } from './bri/authz/authz.module'; @@ -18,6 +17,7 @@ import { WorkgroupsModule } from './bri/workgroup/workgroup.module'; import { ZeroKnowledgeProofModule } from './bri/zeroKnowledgeProof/zeroKnowledgeProof.module'; import { EncryptionModule } from './shared/encryption/encryption.module'; import { LoggingModule } from './shared/logging/logging.module'; +import { PrismaService } from './shared/prisma/prisma.service'; @Module({ imports: [ diff --git a/examples/bri-3/src/bri/authz/guards/authz.guard.ts b/examples/bri-3/src/bri/authz/guards/authz.guard.ts index f07776992..e72b78157 100644 --- a/examples/bri-3/src/bri/authz/guards/authz.guard.ts +++ b/examples/bri-3/src/bri/authz/guards/authz.guard.ts @@ -6,10 +6,10 @@ import { Injectable, } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; -import { PrismaService } from '../../../../prisma/prisma.service'; import { IS_PUBLIC_ENDPOINT_METADATA_KEY } from '../../decorators/public-endpoint'; import { AuthzFactory } from '../authz.factory'; import { CHECK_AUTHZ_METADATA_KEY, IRequirement } from './authz.decorator'; +import { PrismaService } from '../../../shared/prisma/prisma.service'; // helper map to know which relations to include in generic prisma query const prismaTypeToQueryIncludeMap = { diff --git a/examples/bri-3/src/bri/communication/agents/bpiMessagesStorage.agent.ts b/examples/bri-3/src/bri/communication/agents/bpiMessagesStorage.agent.ts index 4b1bee7c0..bd3731656 100644 --- a/examples/bri-3/src/bri/communication/agents/bpiMessagesStorage.agent.ts +++ b/examples/bri-3/src/bri/communication/agents/bpiMessagesStorage.agent.ts @@ -1,21 +1,20 @@ import { Mapper } from '@automapper/core'; import { InjectMapper } from '@automapper/nestjs'; import { Injectable } from '@nestjs/common'; -import { PrismaService } from '../../../../prisma/prisma.service'; import { EncryptionService } from '../../../shared/encryption/encryption.service'; import { BpiMessage } from '../models/bpiMessage'; +import { PrismaService } from '../../../shared/prisma/prisma.service'; @Injectable() -export class BpiMessageStorageAgent extends PrismaService { +export class BpiMessageStorageAgent { constructor( @InjectMapper() private mapper: Mapper, private readonly encryptionService: EncryptionService, - ) { - super(); - } + private readonly prisma: PrismaService, + ) {} async getBpiMessageById(id: string): Promise { - const bpiMessageModel = await this.message.findUnique({ + const bpiMessageModel = await this.prisma.message.findUnique({ where: { id }, include: { fromBpiSubject: true, toBpiSubject: true }, }); @@ -33,7 +32,7 @@ export class BpiMessageStorageAgent extends PrismaService { } async getAllBpiMessages(): Promise { - const bpiMessageModels = await this.message.findMany(); + const bpiMessageModels = await this.prisma.message.findMany(); return bpiMessageModels.map((bpiMessageModel) => { return this.mapper.map(bpiMessageModel, BpiMessage, BpiMessage); @@ -41,7 +40,7 @@ export class BpiMessageStorageAgent extends PrismaService { } async storeNewBpiMessage(bpiMessage: BpiMessage): Promise { - const newBpiMessageModel = await this.message.create({ + const newBpiMessageModel = await this.prisma.message.create({ data: { id: bpiMessage.id, fromBpiSubjectId: bpiMessage.fromBpiSubjectId, @@ -57,7 +56,7 @@ export class BpiMessageStorageAgent extends PrismaService { } async updateBpiMessage(bpiMessage: BpiMessage): Promise { - const updatedBpiMessageModel = await this.message.update({ + const updatedBpiMessageModel = await this.prisma.message.update({ where: { id: bpiMessage.id }, data: { id: bpiMessage.id, @@ -74,7 +73,7 @@ export class BpiMessageStorageAgent extends PrismaService { } async deleteBpiMessage(bpiMessage: BpiMessage): Promise { - await this.message.delete({ + await this.prisma.message.delete({ where: { id: bpiMessage.id }, }); } diff --git a/examples/bri-3/src/bri/communication/communication.module.ts b/examples/bri-3/src/bri/communication/communication.module.ts index 0dcd2876b..f17507315 100644 --- a/examples/bri-3/src/bri/communication/communication.module.ts +++ b/examples/bri-3/src/bri/communication/communication.module.ts @@ -16,6 +16,7 @@ import { GetBpiMessageByIdQueryHandler } from './capabilities/getBpiMessageById/ import { ProcessInboundMessageCommandHandler } from './capabilities/processInboundMessage/processInboundMessageCommand.handler'; import { UpdateBpiMessageCommandHandler } from './capabilities/updateBpiMessage/updateBpiMessageCommand.handler'; import { CommunicationProfile } from './communicaton.profile'; +import { PrismaModule } from '../../shared/prisma/prisma.module'; export const CommandHandlers = [ CreateBpiMessageCommandHandler, @@ -32,6 +33,7 @@ export const QueryHandlers = [GetBpiMessageByIdQueryHandler]; SubjectModule, LoggingModule, EncryptionModule, + PrismaModule, ], controllers: [MessageController], providers: [ diff --git a/examples/bri-3/src/bri/identity/bpiAccounts/accounts.module.ts b/examples/bri-3/src/bri/identity/bpiAccounts/accounts.module.ts index 48c8d6e03..297ec638d 100644 --- a/examples/bri-3/src/bri/identity/bpiAccounts/accounts.module.ts +++ b/examples/bri-3/src/bri/identity/bpiAccounts/accounts.module.ts @@ -11,6 +11,7 @@ import { DeleteBpiAccountCommandHandler } from './capabilities/deleteBpiAccount/ import { GetAllBpiAccountsQueryHandler } from './capabilities/getAllBpiAccounts/getAllBpiAccountQuery.handler'; import { GetBpiAccountByIdQueryHandler } from './capabilities/getBpiAccountById/getBpiAccountByIdQuery.handler'; import { UpdateBpiAccountCommandHandler } from './capabilities/updateBpiAccount/updateBpiAccountCommand.handler'; +import { PrismaModule } from '../../../shared/prisma/prisma.module'; export const CommandHandlers = [ CreateBpiAccountCommandHandler, @@ -22,7 +23,7 @@ export const QueryHandlers = [ GetAllBpiAccountsQueryHandler, ]; @Module({ - imports: [CqrsModule, SubjectAccountModule, MerkleModule], + imports: [CqrsModule, SubjectAccountModule, MerkleModule, PrismaModule], controllers: [AccountController], providers: [ ...CommandHandlers, diff --git a/examples/bri-3/src/bri/identity/bpiAccounts/agents/bpiAccountsStorage.agent.ts b/examples/bri-3/src/bri/identity/bpiAccounts/agents/bpiAccountsStorage.agent.ts index 961ffb49b..fc0e99793 100644 --- a/examples/bri-3/src/bri/identity/bpiAccounts/agents/bpiAccountsStorage.agent.ts +++ b/examples/bri-3/src/bri/identity/bpiAccounts/agents/bpiAccountsStorage.agent.ts @@ -2,23 +2,24 @@ import { Mapper } from '@automapper/core'; import { InjectMapper } from '@automapper/nestjs'; import { Injectable, NotFoundException } from '@nestjs/common'; import MerkleTree from 'merkletreejs'; -import { PrismaService } from '../../../../../prisma/prisma.service'; import { Witness } from '../../../zeroKnowledgeProof/models/witness'; import { NOT_FOUND_ERR_MESSAGE } from '../api/err.messages'; import { BpiAccount } from '../models/bpiAccount'; import { StateTreeLeafValueContent } from '../../../state/models/stateTreeLeafValueContent'; +import { PrismaService } from '../../../../shared/prisma/prisma.service'; // Repositories are the only places that talk the Prisma language of models. // They are always mapped to and from domain objects so that the business layer of the application // does not have to care about the ORM. @Injectable() -export class BpiAccountStorageAgent extends PrismaService { - constructor(@InjectMapper() private readonly mapper: Mapper) { - super(); - } +export class BpiAccountStorageAgent { + constructor( + @InjectMapper() private readonly mapper: Mapper, + private readonly prisma: PrismaService, + ) {} async getAccountById(id: string): Promise { - const bpiAccountModel = await this.bpiAccount.findUnique({ + const bpiAccountModel = await this.prisma.bpiAccount.findUnique({ where: { id }, include: { ownerBpiSubjectAccounts: { @@ -38,7 +39,7 @@ export class BpiAccountStorageAgent extends PrismaService { } async getAllBpiAccounts(): Promise { - const bpiAccountModels = await this.bpiAccount.findMany({ + const bpiAccountModels = await this.prisma.bpiAccount.findMany({ include: { ownerBpiSubjectAccounts: { include: { @@ -62,7 +63,7 @@ export class BpiAccountStorageAgent extends PrismaService { }; }, ); - const newBpiAccountModel = await this.bpiAccount.create({ + const newBpiAccountModel = await this.prisma.bpiAccount.create({ data: { nonce: bpiAccount.nonce, ownerBpiSubjectAccounts: { @@ -91,7 +92,7 @@ export class BpiAccountStorageAgent extends PrismaService { } async updateBpiAccount(bpiAccount: BpiAccount): Promise { - const newBpiAccountModel = await this.bpiAccount.update({ + const newBpiAccountModel = await this.prisma.bpiAccount.update({ where: { id: bpiAccount.id }, data: { nonce: bpiAccount.nonce, @@ -105,7 +106,7 @@ export class BpiAccountStorageAgent extends PrismaService { } async deleteBpiAccount(bpiAccount: BpiAccount): Promise { - await this.bpiAccount.delete({ + await this.prisma.bpiAccount.delete({ where: { id: bpiAccount.id }, }); } @@ -117,7 +118,7 @@ export class BpiAccountStorageAgent extends PrismaService { merkPayload: MerkleTree, witness: Witness, ): Promise { - await this.bpiAccountStateTreeLeafValue.create({ + await this.prisma.bpiAccountStateTreeLeafValue.create({ data: { bpiAccountId: id, leafValue: leafValue, @@ -131,9 +132,10 @@ export class BpiAccountStorageAgent extends PrismaService { async getAccompanyingStateLeafValues( leafValue: string, ): Promise { - const stateLeafValues = await this.bpiAccountStateTreeLeafValue.findUnique({ - where: { leafValue: leafValue }, - }); + const stateLeafValues = + await this.prisma.bpiAccountStateTreeLeafValue.findUnique({ + where: { leafValue: leafValue }, + }); if (!stateLeafValues) { return undefined; diff --git a/examples/bri-3/src/bri/identity/bpiSubjectAccounts/agents/bpiSubjectAccountsStorage.agent.ts b/examples/bri-3/src/bri/identity/bpiSubjectAccounts/agents/bpiSubjectAccountsStorage.agent.ts index 1a00e55f4..4a95c9b38 100644 --- a/examples/bri-3/src/bri/identity/bpiSubjectAccounts/agents/bpiSubjectAccountsStorage.agent.ts +++ b/examples/bri-3/src/bri/identity/bpiSubjectAccounts/agents/bpiSubjectAccountsStorage.agent.ts @@ -1,26 +1,27 @@ import { Mapper } from '@automapper/core'; import { InjectMapper } from '@automapper/nestjs'; -import { Injectable, NotFoundException } from '@nestjs/common'; -import { PrismaService } from '../../../../../prisma/prisma.service'; -import { NOT_FOUND_ERR_MESSAGE } from '../api/err.messages'; +import { Injectable } from '@nestjs/common'; import { BpiSubjectAccount } from '../models/bpiSubjectAccount'; +import { PrismaService } from '../../../../shared/prisma/prisma.service'; // Repositories are the only places that talk the Prisma language of models. // They are always mapped to and from domain objects so that the business layer of the application // does not have to care about the ORM. @Injectable() -export class BpiSubjectAccountStorageAgent extends PrismaService { - constructor(@InjectMapper() private readonly mapper: Mapper) { - super(); - } +export class BpiSubjectAccountStorageAgent { + constructor( + @InjectMapper() private readonly mapper: Mapper, + private readonly prisma: PrismaService, + ) {} async getBpiSubjectAccountById( id: string, ): Promise { - const bpiSubjectAccountModel = await this.bpiSubjectAccount.findUnique({ - where: { id: id }, - include: { ownerBpiSubject: true, creatorBpiSubject: true }, - }); + const bpiSubjectAccountModel = + await this.prisma.bpiSubjectAccount.findUnique({ + where: { id: id }, + include: { ownerBpiSubject: true, creatorBpiSubject: true }, + }); if (!bpiSubjectAccountModel) { return undefined; @@ -34,9 +35,10 @@ export class BpiSubjectAccountStorageAgent extends PrismaService { } async getAllBpiSubjectAccounts(): Promise { - const bpiSubjectAccountsModels = await this.bpiSubjectAccount.findMany({ - include: { ownerBpiSubject: true, creatorBpiSubject: true }, - }); + const bpiSubjectAccountsModels = + await this.prisma.bpiSubjectAccount.findMany({ + include: { ownerBpiSubject: true, creatorBpiSubject: true }, + }); return bpiSubjectAccountsModels.map((bp) => { return this.mapper.map(bp, BpiSubjectAccount, BpiSubjectAccount); }); @@ -45,17 +47,18 @@ export class BpiSubjectAccountStorageAgent extends PrismaService { async storeNewBpiSubjectAccount( bpiSubjectAccount: BpiSubjectAccount, ): Promise { - const newBpiSubjectAccountModel = await this.bpiSubjectAccount.create({ - data: { - creatorBpiSubjectId: bpiSubjectAccount.creatorBpiSubject.id, - ownerBpiSubjectId: bpiSubjectAccount.ownerBpiSubject.id, - authenticationPolicy: bpiSubjectAccount.authenticationPolicy, - authorizationPolicy: bpiSubjectAccount.authorizationPolicy, - verifiableCredential: bpiSubjectAccount.verifiableCredential, - recoveryKey: bpiSubjectAccount.recoveryKey, - }, - include: { ownerBpiSubject: true, creatorBpiSubject: true }, - }); + const newBpiSubjectAccountModel = + await this.prisma.bpiSubjectAccount.create({ + data: { + creatorBpiSubjectId: bpiSubjectAccount.creatorBpiSubject.id, + ownerBpiSubjectId: bpiSubjectAccount.ownerBpiSubject.id, + authenticationPolicy: bpiSubjectAccount.authenticationPolicy, + authorizationPolicy: bpiSubjectAccount.authorizationPolicy, + verifiableCredential: bpiSubjectAccount.verifiableCredential, + recoveryKey: bpiSubjectAccount.recoveryKey, + }, + include: { ownerBpiSubject: true, creatorBpiSubject: true }, + }); return this.mapper.map( newBpiSubjectAccountModel, @@ -67,18 +70,19 @@ export class BpiSubjectAccountStorageAgent extends PrismaService { async updateBpiSubjectAccount( bpiSubjectAccount: BpiSubjectAccount, ): Promise { - const newBpiSubjectAccountModel = await this.bpiSubjectAccount.update({ - where: { id: bpiSubjectAccount.id }, - data: { - creatorBpiSubjectId: bpiSubjectAccount.creatorBpiSubject.id, - ownerBpiSubjectId: bpiSubjectAccount.ownerBpiSubject.id, - authenticationPolicy: bpiSubjectAccount.authenticationPolicy, - authorizationPolicy: bpiSubjectAccount.authorizationPolicy, - verifiableCredential: bpiSubjectAccount.verifiableCredential, - recoveryKey: bpiSubjectAccount.recoveryKey, - }, - include: { ownerBpiSubject: true, creatorBpiSubject: true }, - }); + const newBpiSubjectAccountModel = + await this.prisma.bpiSubjectAccount.update({ + where: { id: bpiSubjectAccount.id }, + data: { + creatorBpiSubjectId: bpiSubjectAccount.creatorBpiSubject.id, + ownerBpiSubjectId: bpiSubjectAccount.ownerBpiSubject.id, + authenticationPolicy: bpiSubjectAccount.authenticationPolicy, + authorizationPolicy: bpiSubjectAccount.authorizationPolicy, + verifiableCredential: bpiSubjectAccount.verifiableCredential, + recoveryKey: bpiSubjectAccount.recoveryKey, + }, + include: { ownerBpiSubject: true, creatorBpiSubject: true }, + }); return this.mapper.map( newBpiSubjectAccountModel, @@ -90,7 +94,7 @@ export class BpiSubjectAccountStorageAgent extends PrismaService { async deleteBpiSubjectAccount( bpiSubjectAccount: BpiSubjectAccount, ): Promise { - await this.bpiSubjectAccount.delete({ + await this.prisma.bpiSubjectAccount.delete({ where: { id: bpiSubjectAccount.id }, }); } diff --git a/examples/bri-3/src/bri/identity/bpiSubjectAccounts/subjectAccounts.module.ts b/examples/bri-3/src/bri/identity/bpiSubjectAccounts/subjectAccounts.module.ts index e2a33e574..d3f5fc8bb 100644 --- a/examples/bri-3/src/bri/identity/bpiSubjectAccounts/subjectAccounts.module.ts +++ b/examples/bri-3/src/bri/identity/bpiSubjectAccounts/subjectAccounts.module.ts @@ -10,6 +10,7 @@ import { GetAllBpiSubjectAccountsQueryHandler } from './capabilities/getAllBpiSu import { GetBpiSubjectAccountByIdQueryHandler } from './capabilities/getBpiSubjectAccountById/getBpiSubjectAccountByIdQuery.handler'; import { UpdateBpiSubjectAccountCommandHandler } from './capabilities/updateBpiSubjectAccount/updateBpiSubjectAccountCommand.handler'; import { SubjectAccountsProfile } from './subjectAccounts.profile'; +import { PrismaModule } from '../../../shared/prisma/prisma.module'; export const CommandHandlers = [ CreateBpiSubjectAccountCommandHandler, @@ -22,7 +23,7 @@ export const QueryHandlers = [ ]; @Module({ - imports: [CqrsModule, SubjectModule], + imports: [CqrsModule, SubjectModule, PrismaModule], controllers: [SubjectAccountController], providers: [ ...CommandHandlers, diff --git a/examples/bri-3/src/bri/identity/bpiSubjects/agents/bpiSubjectsStorage.agent.ts b/examples/bri-3/src/bri/identity/bpiSubjects/agents/bpiSubjectsStorage.agent.ts index e8438d2e8..77453b777 100644 --- a/examples/bri-3/src/bri/identity/bpiSubjects/agents/bpiSubjectsStorage.agent.ts +++ b/examples/bri-3/src/bri/identity/bpiSubjects/agents/bpiSubjectsStorage.agent.ts @@ -1,22 +1,23 @@ import { Injectable, NotFoundException } from '@nestjs/common'; -import { PrismaService } from '../../../../../prisma/prisma.service'; import { NOT_FOUND_ERR_MESSAGE } from '../api/err.messages'; import { BpiSubject } from '../models/bpiSubject'; import { InjectMapper } from '@automapper/nestjs'; import { Mapper } from '@automapper/core'; import { BpiSubjectRole, BpiSubjectRoleName } from '../models/bpiSubjectRole'; +import { PrismaService } from '../../../../shared/prisma/prisma.service'; // Repositories are the only places that talk the Prisma language of models. // They are always mapped to and from domain objects so that the business layer of the application // does not have to care about the ORM. @Injectable() -export class BpiSubjectStorageAgent extends PrismaService { - constructor(@InjectMapper() private mapper: Mapper) { - super(); - } +export class BpiSubjectStorageAgent { + constructor( + @InjectMapper() private mapper: Mapper, + private readonly prisma: PrismaService, + ) {} async getBpiSubjectById(id: string): Promise { - const bpiSubjectModel = await this.bpiSubject.findUnique({ + const bpiSubjectModel = await this.prisma.bpiSubject.findUnique({ where: { id }, }); @@ -28,14 +29,14 @@ export class BpiSubjectStorageAgent extends PrismaService { } async getAllBpiSubjects(): Promise { - const bpiSubjectModels = await this.bpiSubject.findMany(); + const bpiSubjectModels = await this.prisma.bpiSubject.findMany(); return bpiSubjectModels.map((bpiSubjectModel) => { return this.mapper.map(bpiSubjectModel, BpiSubject, BpiSubject); }); } async getBpiSubjectsById(ids: string[]): Promise { - const bpiSubjectModels = await this.bpiSubject.findMany({ + const bpiSubjectModels = await this.prisma.bpiSubject.findMany({ where: { id: { in: ids }, }, @@ -49,7 +50,7 @@ export class BpiSubjectStorageAgent extends PrismaService { async getBpiSubjectRoleByName( name: BpiSubjectRoleName, ): Promise { - const bpiSubjectRole = await this.bpiSubjectRole.findUnique({ + const bpiSubjectRole = await this.prisma.bpiSubjectRole.findUnique({ where: { name }, }); @@ -61,7 +62,7 @@ export class BpiSubjectStorageAgent extends PrismaService { async storeNewBpiSubject(bpiSubject: BpiSubject): Promise { bpiSubject.publicKey = bpiSubject.publicKey.toLowerCase(); - const newBpiSubjectModel = await this.bpiSubject.create({ + const newBpiSubjectModel = await this.prisma.bpiSubject.create({ data: { ...bpiSubject, roles: { @@ -78,7 +79,7 @@ export class BpiSubjectStorageAgent extends PrismaService { } async updateBpiSubject(bpiSubject: BpiSubject): Promise { - const updatedBpiSubjectModel = await this.bpiSubject.update({ + const updatedBpiSubjectModel = await this.prisma.bpiSubject.update({ where: { id: bpiSubject.id }, data: { ...bpiSubject, @@ -95,13 +96,13 @@ export class BpiSubjectStorageAgent extends PrismaService { } async deleteBpiSubject(bpiSubject: BpiSubject): Promise { - await this.bpiSubject.delete({ + await this.prisma.bpiSubject.delete({ where: { id: bpiSubject.id }, }); } async getBpiSubjectByPublicKey(publicKey: string): Promise { - const bpiSubjectModel = await this.bpiSubject.findFirst({ + const bpiSubjectModel = await this.prisma.bpiSubject.findFirst({ where: { publicKey: publicKey, }, diff --git a/examples/bri-3/src/bri/identity/bpiSubjects/subjects.module.ts b/examples/bri-3/src/bri/identity/bpiSubjects/subjects.module.ts index 23327bac3..866473504 100644 --- a/examples/bri-3/src/bri/identity/bpiSubjects/subjects.module.ts +++ b/examples/bri-3/src/bri/identity/bpiSubjects/subjects.module.ts @@ -10,6 +10,7 @@ import { UpdateBpiSubjectCommandHandler } from './capabilities/updateBpiSubject/ import { BpiSubjectStorageAgent } from './agents/bpiSubjectsStorage.agent'; import { SubjectsProfile } from './subjects.profile'; import { AuthzModule } from '../../authz/authz.module'; +import { PrismaModule } from '../../../shared/prisma/prisma.module'; export const CommandHandlers = [ CreateBpiSubjectCommandHandler, @@ -22,7 +23,7 @@ export const QueryHandlers = [ ]; @Module({ - imports: [CqrsModule, AuthzModule], + imports: [CqrsModule, AuthzModule, PrismaModule], controllers: [SubjectController], providers: [ ...CommandHandlers, diff --git a/examples/bri-3/src/bri/merkleTree/agents/import MerkleTree from 'merkletreejs';.ts b/examples/bri-3/src/bri/merkleTree/agents/import MerkleTree from 'merkletreejs';.ts deleted file mode 100644 index ebb519278..000000000 --- a/examples/bri-3/src/bri/merkleTree/agents/import MerkleTree from 'merkletreejs';.ts +++ /dev/null @@ -1,20 +0,0 @@ -import MerkleTree from 'merkletreejs'; -import * as crypto from 'crypto'; - -const leaves = ['leave1', 'leave2', 'leave3']; - -const hashFn = (data: any) => { - return data; -}; - -const hashedLeaves = leaves.map((l) => hashFn(l)); - -const myTree = new MerkleTree(hashedLeaves, hashFn); - -console.log(MerkleTree.marshalTree(myTree)); - -myTree.addLeaf(hashFn('leave4')); - -console.log(MerkleTree.marshalTree(myTree)); - -console.log(myTree.getLeaf(0)); diff --git a/examples/bri-3/src/bri/merkleTree/agents/merkleTreeStorage.agent.ts b/examples/bri-3/src/bri/merkleTree/agents/merkleTreeStorage.agent.ts index 3c80ba548..6156266c9 100644 --- a/examples/bri-3/src/bri/merkleTree/agents/merkleTreeStorage.agent.ts +++ b/examples/bri-3/src/bri/merkleTree/agents/merkleTreeStorage.agent.ts @@ -1,19 +1,20 @@ import { Injectable } from '@nestjs/common'; import { InjectMapper } from '@automapper/nestjs'; import { Mapper } from '@automapper/core'; -import { PrismaService } from '../../../../prisma/prisma.service'; import MerkleTree from 'merkletreejs'; import { BpiMerkleTree } from '../models/bpiMerkleTree'; import { MerkleTreeDto } from '../api/dtos/response/merkleTree.dto'; +import { PrismaService } from '../../../shared/prisma/prisma.service'; @Injectable() -export class MerkleTreeStorageAgent extends PrismaService { - constructor(@InjectMapper() private mapper: Mapper) { - super(); - } +export class MerkleTreeStorageAgent { + constructor( + @InjectMapper() private mapper: Mapper, + private readonly prisma: PrismaService, + ) {} async getMerkleTreeById(id: string): Promise { - const merkleTreeModel = await this.bpiMerkleTree.findUnique({ + const merkleTreeModel = await this.prisma.bpiMerkleTree.findUnique({ where: { id }, }); @@ -25,7 +26,7 @@ export class MerkleTreeStorageAgent extends PrismaService { } async storeNewMerkleTree(merkleTree: BpiMerkleTree): Promise { - const storedMerkleTree = await this.bpiMerkleTree.create({ + const storedMerkleTree = await this.prisma.bpiMerkleTree.create({ data: { id: merkleTree.id, hashAlgName: merkleTree.hashAlgName, @@ -39,7 +40,7 @@ export class MerkleTreeStorageAgent extends PrismaService { async storeUpdatedMerkleTree( merkleTree: BpiMerkleTree, ): Promise { - const updatedMerkleTree = await this.bpiMerkleTree.update({ + const updatedMerkleTree = await this.prisma.bpiMerkleTree.update({ where: { id: merkleTree.id }, data: { tree: MerkleTree.marshalTree(merkleTree.tree), @@ -50,7 +51,7 @@ export class MerkleTreeStorageAgent extends PrismaService { } async deleteMerkleTree(merkleTree: BpiMerkleTree): Promise { - await this.bpiMerkleTree.delete({ + await this.prisma.bpiMerkleTree.delete({ where: { id: merkleTree.id }, }); diff --git a/examples/bri-3/src/bri/merkleTree/merkle.module.ts b/examples/bri-3/src/bri/merkleTree/merkle.module.ts index 81e650430..d3359c5b8 100644 --- a/examples/bri-3/src/bri/merkleTree/merkle.module.ts +++ b/examples/bri-3/src/bri/merkleTree/merkle.module.ts @@ -7,9 +7,16 @@ import { MerkleTreeAgent } from './agents/merkleTree.agent'; import { MerkleTreeStorageAgent } from './agents/merkleTreeStorage.agent'; import { MerkleProfile } from './merkle.profile'; import { MerkleTreeService } from './services/merkleTree.service'; +import { PrismaModule } from '../../shared/prisma/prisma.module'; @Module({ - imports: [CqrsModule, AuthModule, LoggingModule, EncryptionModule], + imports: [ + CqrsModule, + AuthModule, + LoggingModule, + EncryptionModule, + PrismaModule, + ], providers: [ MerkleTreeAgent, MerkleTreeStorageAgent, diff --git a/examples/bri-3/src/bri/transactions/agents/transactionStorage.agent.ts b/examples/bri-3/src/bri/transactions/agents/transactionStorage.agent.ts index ca68163a0..017150f4e 100644 --- a/examples/bri-3/src/bri/transactions/agents/transactionStorage.agent.ts +++ b/examples/bri-3/src/bri/transactions/agents/transactionStorage.agent.ts @@ -1,19 +1,20 @@ import { Mapper } from '@automapper/core'; import { InjectMapper } from '@automapper/nestjs'; import { Injectable, NotFoundException } from '@nestjs/common'; -import { PrismaService } from '../../../../prisma/prisma.service'; import { NOT_FOUND_ERR_MESSAGE } from '..//api/err.messages'; import { Transaction } from '../models/transaction'; import { TransactionStatus } from '../models/transactionStatus.enum'; +import { PrismaService } from '../../../shared/prisma/prisma.service'; @Injectable() -export class TransactionStorageAgent extends PrismaService { - constructor(@InjectMapper() private mapper: Mapper) { - super(); - } +export class TransactionStorageAgent { + constructor( + @InjectMapper() private mapper: Mapper, + private readonly prisma: PrismaService, + ) {} async getAllTransactions(): Promise { - const transactionModels = await this.transaction.findMany({ + const transactionModels = await this.prisma.transaction.findMany({ include: { fromBpiSubjectAccount: true, toBpiSubjectAccount: true }, }); return transactionModels.map((transactionModel) => { @@ -22,7 +23,7 @@ export class TransactionStorageAgent extends PrismaService { } async getTransactionById(id: string): Promise { - const transactionModel = await this.transaction.findUnique({ + const transactionModel = await this.prisma.transaction.findUnique({ where: { id }, include: { fromBpiSubjectAccount: { @@ -53,7 +54,7 @@ export class TransactionStorageAgent extends PrismaService { ): Promise { // TODO: #745 Add creation date to transaction // TODO: #745 Add execution or abortion date to transaction - const transactionModels = await this.transaction.findMany({ + const transactionModels = await this.prisma.transaction.findMany({ include: { fromBpiSubjectAccount: { include: { @@ -76,7 +77,7 @@ export class TransactionStorageAgent extends PrismaService { } async storeNewTransaction(transaction: Transaction): Promise { - const newTransactionModel = await this.transaction.create({ + const newTransactionModel = await this.prisma.transaction.create({ data: { id: transaction.id, nonce: transaction.nonce, @@ -108,7 +109,7 @@ export class TransactionStorageAgent extends PrismaService { } async updateTransaction(transaction: Transaction): Promise { - const updatedTransactionModel = await this.transaction.update({ + const updatedTransactionModel = await this.prisma.transaction.update({ where: { id: transaction.id }, data: { payload: transaction.payload, @@ -122,7 +123,7 @@ export class TransactionStorageAgent extends PrismaService { async updateTransactionStatus( transaction: Transaction, ): Promise { - const updatedTransaction = await this.transaction.update({ + const updatedTransaction = await this.prisma.transaction.update({ where: { id: transaction.id, }, @@ -135,7 +136,7 @@ export class TransactionStorageAgent extends PrismaService { } async deleteTransaction(transaction: Transaction): Promise { - await this.transaction.delete({ + await this.prisma.transaction.delete({ where: { id: transaction.id }, }); } diff --git a/examples/bri-3/src/bri/transactions/transactions.module.ts b/examples/bri-3/src/bri/transactions/transactions.module.ts index e0fb06540..e4ad33764 100644 --- a/examples/bri-3/src/bri/transactions/transactions.module.ts +++ b/examples/bri-3/src/bri/transactions/transactions.module.ts @@ -15,6 +15,7 @@ import { GetTransactionByIdQueryHandler } from './capabilities/getTransactionByI import { UpdateTransactionCommandHandler } from './capabilities/updateTransaction/updateTransactionCommand.handler'; import { TransactionsProfile } from './transactions.profile'; import { MerkleModule } from '../merkleTree/merkle.module'; +import { PrismaModule } from '../../shared/prisma/prisma.module'; export const CommandHandlers = [ CreateTransactionCommandHandler, @@ -36,6 +37,7 @@ export const QueryHandlers = [ AuthModule, MerkleModule, ZeroKnowledgeProofModule, + PrismaModule, ], controllers: [TransactionController], providers: [ diff --git a/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts b/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts index 6f81b225b..0cc0215db 100644 --- a/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts +++ b/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts @@ -1,18 +1,19 @@ import { Mapper } from '@automapper/core'; import { InjectMapper } from '@automapper/nestjs'; import { Injectable, NotFoundException } from '@nestjs/common'; -import { PrismaService } from '../../../../../prisma/prisma.service'; import { WORKFLOW_NOT_FOUND_ERR_MESSAGE } from '../api/err.messages'; import { Workflow } from '../models/workflow'; +import { PrismaService } from '../../../../shared/prisma/prisma.service'; @Injectable() -export class WorkflowStorageAgent extends PrismaService { - constructor(@InjectMapper() private mapper: Mapper) { - super(); - } +export class WorkflowStorageAgent { + constructor( + @InjectMapper() private mapper: Mapper, + private readonly prisma: PrismaService, + ) {} async getWorkflowById(id: string): Promise { - const workflowModel = await this.workflow.findUnique({ + const workflowModel = await this.prisma.workflow.findUnique({ where: { id: id }, include: { worksteps: true, @@ -28,7 +29,7 @@ export class WorkflowStorageAgent extends PrismaService { } async getAllWorkflows(): Promise { - const workflowModels = await this.workflow.findMany({ + const workflowModels = await this.prisma.workflow.findMany({ include: { worksteps: true }, }); return workflowModels.map((w) => { @@ -37,7 +38,7 @@ export class WorkflowStorageAgent extends PrismaService { } async getWorkflowsByIds(ids: string[]): Promise { - const workflowModels = await this.workflow.findMany({ + const workflowModels = await this.prisma.workflow.findMany({ where: { id: { in: ids }, }, @@ -55,7 +56,7 @@ export class WorkflowStorageAgent extends PrismaService { }; }); - const newWorkflowModel = await this.workflow.create({ + const newWorkflowModel = await this.prisma.workflow.create({ data: { id: workflow.id, name: workflow.name, @@ -81,7 +82,7 @@ export class WorkflowStorageAgent extends PrismaService { }; }); - const updatedWorkflowModel = await this.workflow.update({ + const updatedWorkflowModel = await this.prisma.workflow.update({ where: { id: workflow.id }, data: { name: workflow.name, @@ -99,7 +100,7 @@ export class WorkflowStorageAgent extends PrismaService { } async deleteWorkflow(workflow: Workflow): Promise { - await this.workflow.delete({ + await this.prisma.workflow.delete({ where: { id: workflow.id }, }); } diff --git a/examples/bri-3/src/bri/workgroup/workflows/workflows.module.ts b/examples/bri-3/src/bri/workgroup/workflows/workflows.module.ts index 8e5fc6f54..455319cf4 100644 --- a/examples/bri-3/src/bri/workgroup/workflows/workflows.module.ts +++ b/examples/bri-3/src/bri/workgroup/workflows/workflows.module.ts @@ -15,6 +15,7 @@ import { GetAllWorkflowsQueryHandler } from './capabilities/getAllWorkflows/getA import { GetWorkflowByIdQueryHandler } from './capabilities/getWorkflowById/getWorkflowByIdQuery.handler'; import { UpdateWorkflowCommandHandler } from './capabilities/updateWorkflow/updateWorkflowCommand.handler'; import { WorkflowProfile } from './workflow.profile'; +import { PrismaModule } from '../../../shared/prisma/prisma.module'; export const CommandHandlers = [ CreateWorkflowCommandHandler, @@ -34,6 +35,7 @@ export const QueryHandlers = [ AccountModule, SubjectAccountModule, SubjectModule, + PrismaModule, ], controllers: [WorkflowController], providers: [ diff --git a/examples/bri-3/src/bri/workgroup/workgroups/agents/workgroupStorage.agent.ts b/examples/bri-3/src/bri/workgroup/workgroups/agents/workgroupStorage.agent.ts index 942d519d4..f586e8881 100644 --- a/examples/bri-3/src/bri/workgroup/workgroups/agents/workgroupStorage.agent.ts +++ b/examples/bri-3/src/bri/workgroup/workgroups/agents/workgroupStorage.agent.ts @@ -1,18 +1,19 @@ import { Injectable, NotFoundException } from '@nestjs/common'; -import { PrismaService } from '../../../../../prisma/prisma.service'; import { Workgroup } from '../models/workgroup'; import { WORKGROUP_NOT_FOUND_ERR_MESSAGE } from '../api/err.messages'; import { InjectMapper } from '@automapper/nestjs'; import { Mapper } from '@automapper/core'; +import { PrismaService } from '../../../../shared/prisma/prisma.service'; @Injectable() -export class WorkgroupStorageAgent extends PrismaService { - constructor(@InjectMapper() private mapper: Mapper) { - super(); - } +export class WorkgroupStorageAgent { + constructor( + @InjectMapper() private mapper: Mapper, + private readonly prisma: PrismaService, + ) {} async getWorkgroupById(id: string): Promise { - const workgroupModel = await this.workgroup.findUnique({ + const workgroupModel = await this.prisma.workgroup.findUnique({ where: { id: id }, include: { worksteps: true, @@ -44,7 +45,7 @@ export class WorkgroupStorageAgent extends PrismaService { }; }); - const newWorkgroupModel = await this.workgroup.create({ + const newWorkgroupModel = await this.prisma.workgroup.create({ data: { id: workgroup.id, name: workgroup.name, @@ -83,7 +84,7 @@ export class WorkgroupStorageAgent extends PrismaService { }; }); - const updatedWorkgroupModel = await this.workgroup.update({ + const updatedWorkgroupModel = await this.prisma.workgroup.update({ where: { id: workgroup.id }, data: { name: workgroup.name, @@ -109,7 +110,7 @@ export class WorkgroupStorageAgent extends PrismaService { } async deleteWorkgroup(workgroup: Workgroup): Promise { - await this.workgroup.delete({ + await this.prisma.workgroup.delete({ where: { id: workgroup.id }, }); } diff --git a/examples/bri-3/src/bri/workgroup/workgroups/workgroups.module.ts b/examples/bri-3/src/bri/workgroup/workgroups/workgroups.module.ts index 1ea119067..90e7ecddc 100644 --- a/examples/bri-3/src/bri/workgroup/workgroups/workgroups.module.ts +++ b/examples/bri-3/src/bri/workgroup/workgroups/workgroups.module.ts @@ -1,7 +1,6 @@ import { Module } from '@nestjs/common'; import { CqrsModule } from '@nestjs/cqrs'; import { AuthModule } from '../../auth/auth.module'; -import { BpiSubjectAgent } from '../../identity/bpiSubjects/agents/bpiSubjects.agent'; import { SubjectModule } from '../../identity/bpiSubjects/subjects.module'; import { WorkflowModule } from '../workflows/workflows.module'; import { WorkstepModule } from '../worksteps/worksteps.module'; @@ -14,6 +13,7 @@ import { DeleteWorkgroupCommandHandler } from './capabilities/deleteWorkgroup/de import { GetWorkgroupByIdQueryHandler } from './capabilities/getWorkgroupById/getWorkgroupByIdQuery.handler'; import { UpdateWorkgroupCommandHandler } from './capabilities/updateWorkgroup/updateWorkgroupCommand.handler'; import { WorkgroupProfile } from './workgroups.profile'; +import { PrismaModule } from '../../../shared/prisma/prisma.module'; export const CommandHandlers = [ CreateWorkgroupCommandHandler, @@ -30,6 +30,7 @@ export const QueryHandlers = [GetWorkgroupByIdQueryHandler]; WorkstepModule, WorkflowModule, AuthModule, + PrismaModule, ], controllers: [WorkgroupController], providers: [ diff --git a/examples/bri-3/src/bri/workgroup/worksteps/agents/workstepsStorage.agent.ts b/examples/bri-3/src/bri/workgroup/worksteps/agents/workstepsStorage.agent.ts index 07615c221..d8abc1d7b 100644 --- a/examples/bri-3/src/bri/workgroup/worksteps/agents/workstepsStorage.agent.ts +++ b/examples/bri-3/src/bri/workgroup/worksteps/agents/workstepsStorage.agent.ts @@ -1,21 +1,24 @@ import { Injectable, NotFoundException } from '@nestjs/common'; -import { PrismaService } from '../../../../../prisma/prisma.service'; import { NOT_FOUND_ERR_MESSAGE } from '../api/err.messages'; import { Workstep } from '../models/workstep'; import { InjectMapper } from '@automapper/nestjs'; import { Mapper } from '@automapper/core'; +import { PrismaService } from '../../../../shared/prisma/prisma.service'; // Storage Agents are the only places that talk the Prisma language of models. // They are always mapped to and from domain objects so that the business layer of the application // does not have to care about the ORM. @Injectable() -export class WorkstepStorageAgent extends PrismaService { - constructor(@InjectMapper() private readonly mapper: Mapper) { - super(); - } +export class WorkstepStorageAgent { + constructor( + @InjectMapper() private readonly mapper: Mapper, + private readonly prisma: PrismaService, + ) {} async getWorkstepById(id: string): Promise { - const workstepModel = await this.workstep.findUnique({ where: { id } }); + const workstepModel = await this.prisma.workstep.findUnique({ + where: { id }, + }); if (!workstepModel) { throw new NotFoundException(NOT_FOUND_ERR_MESSAGE); @@ -25,14 +28,14 @@ export class WorkstepStorageAgent extends PrismaService { } async getAllWorksteps(): Promise { - const workstepModels = await this.workstep.findMany(); + const workstepModels = await this.prisma.workstep.findMany(); return workstepModels.map((workstepModel) => { return this.mapper.map(workstepModel, Workstep, Workstep); }); } async getMatchingWorkstepsById(ids: string[]): Promise { - const workstepModels = await this.workstep.findMany({ + const workstepModels = await this.prisma.workstep.findMany({ where: { id: { in: ids }, }, @@ -43,14 +46,14 @@ export class WorkstepStorageAgent extends PrismaService { } async storeNewWorkstep(workstep: Workstep): Promise { - const newWorkstepModel = await this.workstep.create({ + const newWorkstepModel = await this.prisma.workstep.create({ data: workstep, }); return this.mapper.map(newWorkstepModel, Workstep, Workstep); } async updateWorkstep(workstep: Workstep): Promise { - const updatedWorkstepModel = await this.workstep.update({ + const updatedWorkstepModel = await this.prisma.workstep.update({ where: { id: workstep.id }, data: workstep, }); @@ -58,7 +61,7 @@ export class WorkstepStorageAgent extends PrismaService { } async deleteWorkstep(workstep: Workstep): Promise { - await this.workstep.delete({ + await this.prisma.workstep.delete({ where: { id: workstep.id }, }); } diff --git a/examples/bri-3/src/bri/workgroup/worksteps/worksteps.module.ts b/examples/bri-3/src/bri/workgroup/worksteps/worksteps.module.ts index 8ec9e5655..bd51824e2 100644 --- a/examples/bri-3/src/bri/workgroup/worksteps/worksteps.module.ts +++ b/examples/bri-3/src/bri/workgroup/worksteps/worksteps.module.ts @@ -10,6 +10,7 @@ import { UpdateWorkstepCommandHandler } from './capabilities/updateWorkstep/upda import { WorkstepStorageAgent } from './agents/workstepsStorage.agent'; import { LoggingModule } from '../../../../src/shared/logging/logging.module'; import { WorkstepProfile } from './workstep.profile'; +import { PrismaModule } from '../../../shared/prisma/prisma.module'; export const CommandHandlers = [ CreateWorkstepCommandHandler, @@ -23,7 +24,7 @@ export const QueryHandlers = [ ]; @Module({ - imports: [CqrsModule, LoggingModule], + imports: [CqrsModule, LoggingModule, PrismaModule], controllers: [WorkstepController], providers: [ ...CommandHandlers, diff --git a/examples/bri-3/src/shared/prisma/prisma.module.ts b/examples/bri-3/src/shared/prisma/prisma.module.ts new file mode 100644 index 000000000..ec0ce3291 --- /dev/null +++ b/examples/bri-3/src/shared/prisma/prisma.module.ts @@ -0,0 +1,8 @@ +import { Module } from '@nestjs/common'; +import { PrismaService } from './prisma.service'; + +@Module({ + providers: [PrismaService], + exports: [PrismaService], +}) +export class PrismaModule {} diff --git a/examples/bri-3/prisma/prisma.service.ts b/examples/bri-3/src/shared/prisma/prisma.service.ts similarity index 82% rename from examples/bri-3/prisma/prisma.service.ts rename to examples/bri-3/src/shared/prisma/prisma.service.ts index 359f950b7..c19aa9f32 100644 --- a/examples/bri-3/prisma/prisma.service.ts +++ b/examples/bri-3/src/shared/prisma/prisma.service.ts @@ -3,6 +3,9 @@ import { PrismaClient } from '@prisma/client'; @Injectable() export class PrismaService extends PrismaClient implements OnModuleInit { + constructor() { + super({ log: ['info'] }); + } async onModuleInit() { await this.$connect(); } From 2ee473e2f8f562ee0a907f0ed3437c6bed270c8a Mon Sep 17 00:00:00 2001 From: Kasshern Date: Mon, 13 Nov 2023 13:04:44 -0600 Subject: [PATCH 02/10] Adds prisma service override provider to spec tests that instantiate postgresql pool --- .../src/bri/communication/api/messages.controller.spec.ts | 5 ++++- .../bri/identity/bpiAccounts/api/accounts.controller.spec.ts | 4 ++++ .../bri/workgroup/workflows/api/workflows.controller.spec.ts | 4 ++++ .../workgroup/workgroups/api/workgroups.controller.spec.ts | 5 +++++ 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/examples/bri-3/src/bri/communication/api/messages.controller.spec.ts b/examples/bri-3/src/bri/communication/api/messages.controller.spec.ts index cccfe90d5..36ee0fc6b 100644 --- a/examples/bri-3/src/bri/communication/api/messages.controller.spec.ts +++ b/examples/bri-3/src/bri/communication/api/messages.controller.spec.ts @@ -34,7 +34,8 @@ import { } from './err.messages'; import { MessageController } from './messages.controller'; import { mockDeep, DeepMockProxy } from 'jest-mock-extended'; -import { ethers } from 'ethers'; +import { PrismaService } from '../../../shared/prisma/prisma.service'; +import { PrismaClient } from '@prisma/client'; describe('MessageController', () => { let mController: MessageController; @@ -108,6 +109,8 @@ describe('MessageController', () => { .useValue(mockDeep()) .overrideProvider(BpiSubjectStorageAgent) .useValue(mockDeep()) + .overrideProvider(PrismaService) + .useValue(mockDeep()) .compile(); mController = app.get(MessageController); diff --git a/examples/bri-3/src/bri/identity/bpiAccounts/api/accounts.controller.spec.ts b/examples/bri-3/src/bri/identity/bpiAccounts/api/accounts.controller.spec.ts index 46bdfdfc8..d7347861c 100644 --- a/examples/bri-3/src/bri/identity/bpiAccounts/api/accounts.controller.spec.ts +++ b/examples/bri-3/src/bri/identity/bpiAccounts/api/accounts.controller.spec.ts @@ -31,6 +31,8 @@ import { BpiAccount } from '../models/bpiAccount'; import { AccountController } from './accounts.controller'; import { CreateBpiAccountDto } from './dtos/request/createBpiAccount.dto'; import { NOT_FOUND_ERR_MESSAGE } from './err.messages'; +import { PrismaService } from '../../../../shared/prisma/prisma.service'; +import { PrismaClient } from '@prisma/client'; describe('AccountController', () => { let accountController: AccountController; @@ -76,6 +78,8 @@ describe('AccountController', () => { .useValue(mockDeep()) .overrideProvider(MerkleTreeStorageAgent) .useValue(mockDeep()) + .overrideProvider(PrismaService) + .useValue(mockDeep()) .compile(); accountController = app.get(AccountController); diff --git a/examples/bri-3/src/bri/workgroup/workflows/api/workflows.controller.spec.ts b/examples/bri-3/src/bri/workgroup/workflows/api/workflows.controller.spec.ts index 1e2b0dd31..a54ca5d4d 100644 --- a/examples/bri-3/src/bri/workgroup/workflows/api/workflows.controller.spec.ts +++ b/examples/bri-3/src/bri/workgroup/workflows/api/workflows.controller.spec.ts @@ -27,6 +27,8 @@ import { WorkflowProfile } from '../workflow.profile'; import { CreateWorkflowDto } from './dtos/request/createWorkflow.dto'; import { UpdateWorkflowDto } from './dtos/request/updateWorkflow.dto'; import { WorkflowController } from './workflows.controller'; +import { PrismaService } from '../../../../shared/prisma/prisma.service'; +import { PrismaClient } from '@prisma/client'; describe('WorkflowsController', () => { let workflowController: WorkflowController; @@ -121,6 +123,8 @@ describe('WorkflowsController', () => { .useValue(mockDeep()) .overrideProvider(BpiAccountStorageAgent) .useValue(mockDeep()) + .overrideProvider(PrismaService) + .useValue(mockDeep()) .compile(); workflowController = app.get(WorkflowController); diff --git a/examples/bri-3/src/bri/workgroup/workgroups/api/workgroups.controller.spec.ts b/examples/bri-3/src/bri/workgroup/workgroups/api/workgroups.controller.spec.ts index b49ac9ca3..7089bd0b4 100644 --- a/examples/bri-3/src/bri/workgroup/workgroups/api/workgroups.controller.spec.ts +++ b/examples/bri-3/src/bri/workgroup/workgroups/api/workgroups.controller.spec.ts @@ -26,6 +26,8 @@ import { Workgroup, WorkgroupStatus } from '../models/workgroup'; import { mockDeep, DeepMockProxy } from 'jest-mock-extended'; import { uuid } from 'uuidv4'; import { WorkgroupProfile } from '../workgroups.profile'; +import { PrismaService } from '../../../../shared/prisma/prisma.service'; +import { PrismaClient } from '@prisma/client'; describe('WorkgroupsController', () => { let workgroupController: WorkgroupController; @@ -73,12 +75,15 @@ describe('WorkgroupsController', () => { GetWorkgroupByIdQueryHandler, WorkgroupStorageAgent, WorkgroupProfile, + PrismaService, ], }) .overrideProvider(WorkgroupStorageAgent) .useValue(mockDeep()) .overrideProvider(BpiSubjectStorageAgent) .useValue(mockDeep()) + .overrideProvider(PrismaService) + .useValue(mockDeep()) .compile(); workgroupController = app.get(WorkgroupController); From e5b56c32f7e29f5c3896dc08ee6cc9b3b0a4ead8 Mon Sep 17 00:00:00 2001 From: Kasshern Date: Wed, 29 Nov 2023 15:20:36 -0600 Subject: [PATCH 03/10] feat: creates execute prisma transaction function in prisma service and converts POC prisma actions into prisma promises within respective storage agents --- .../agents/bpiAccountsStorage.agent.ts | 8 ++++---- .../agents/workflowsStorage.agent.ts | 19 +++++++++++++++---- .../createWorkflowCommand.handler.ts | 17 ++++++++++------- .../bri-3/src/shared/prisma/prisma.service.ts | 18 +++++++++++++----- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/examples/bri-3/src/bri/identity/bpiAccounts/agents/bpiAccountsStorage.agent.ts b/examples/bri-3/src/bri/identity/bpiAccounts/agents/bpiAccountsStorage.agent.ts index fc0e99793..83ce7dc2f 100644 --- a/examples/bri-3/src/bri/identity/bpiAccounts/agents/bpiAccountsStorage.agent.ts +++ b/examples/bri-3/src/bri/identity/bpiAccounts/agents/bpiAccountsStorage.agent.ts @@ -5,6 +5,7 @@ import MerkleTree from 'merkletreejs'; import { Witness } from '../../../zeroKnowledgeProof/models/witness'; import { NOT_FOUND_ERR_MESSAGE } from '../api/err.messages'; import { BpiAccount } from '../models/bpiAccount'; +import { BpiAccount as BpiAccountModel, PrismaPromise } from '@prisma/client'; import { StateTreeLeafValueContent } from '../../../state/models/stateTreeLeafValueContent'; import { PrismaService } from '../../../../shared/prisma/prisma.service'; @@ -55,7 +56,7 @@ export class BpiAccountStorageAgent { }); } - async storeNewBpiAccount(bpiAccount: BpiAccount): Promise { + storeNewBpiAccount(bpiAccount: BpiAccount): PrismaPromise { const connectedOwnerBpiAccounts = bpiAccount.ownerBpiSubjectAccounts.map( (o) => { return { @@ -63,7 +64,8 @@ export class BpiAccountStorageAgent { }; }, ); - const newBpiAccountModel = await this.prisma.bpiAccount.create({ + + return this.prisma.bpiAccount.create({ data: { nonce: bpiAccount.nonce, ownerBpiSubjectAccounts: { @@ -87,8 +89,6 @@ export class BpiAccountStorageAgent { }, }, }); - - return this.mapper.map(newBpiAccountModel, BpiAccount, BpiAccount); } async updateBpiAccount(bpiAccount: BpiAccount): Promise { diff --git a/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts b/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts index 0cc0215db..7602520ef 100644 --- a/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts +++ b/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts @@ -3,7 +3,10 @@ import { InjectMapper } from '@automapper/nestjs'; import { Injectable, NotFoundException } from '@nestjs/common'; import { WORKFLOW_NOT_FOUND_ERR_MESSAGE } from '../api/err.messages'; import { Workflow } from '../models/workflow'; +import { Workflow as WorkflowModel } from '@prisma/client'; +import { BpiAccount as BpiAccountModel } from '@prisma/client'; import { PrismaService } from '../../../../shared/prisma/prisma.service'; +import { PrismaPromise } from '@prisma/client'; @Injectable() export class WorkflowStorageAgent { @@ -49,14 +52,14 @@ export class WorkflowStorageAgent { }); } - async storeNewWorkflow(workflow: Workflow): Promise { + storeNewWorkflow(workflow: Workflow): PrismaPromise { const workstepIds = workflow.worksteps.map((w) => { return { id: w.id, }; }); - const newWorkflowModel = await this.prisma.workflow.create({ + return this.prisma.workflow.create({ data: { id: workflow.id, name: workflow.name, @@ -71,8 +74,6 @@ export class WorkflowStorageAgent { bpiAccount: true, }, }); - - return this.mapper.map(newWorkflowModel, Workflow, Workflow); } async updateWorkflow(workflow: Workflow): Promise { @@ -104,4 +105,14 @@ export class WorkflowStorageAgent { where: { id: workflow.id }, }); } + + async storeWorkflowTransaction( + bpiAccountOperation: PrismaPromise, + workflowOperation: PrismaPromise, + ) { + await this.prisma.executeTransaction( + bpiAccountOperation, + workflowOperation, + ); + } } diff --git a/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts b/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts index 2f2fd5df1..970d0674c 100644 --- a/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts +++ b/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts @@ -47,21 +47,24 @@ export class CreateWorkflowCommandHandler 'sample state object prover system', ); - const newBpiAccount = await this.accountStorageAgent.storeNewBpiAccount( - newBpiAccountCandidate, - ); + const storeNewBpiAccountOperation = + this.accountStorageAgent.storeNewBpiAccount(newBpiAccountCandidate); const newWorkflowCandidate = this.agent.createNewWorkflow( command.name, workstepsToConnect, command.workgroupId, - newBpiAccount, + newBpiAccountCandidate, ); - const newWorkflow = await this.storageAgent.storeNewWorkflow( - newWorkflowCandidate, + const storeNewWorkflowOperation = + this.storageAgent.storeNewWorkflow(newWorkflowCandidate); + + this.storageAgent.storeWorkflowTransaction( + storeNewBpiAccountOperation, + storeNewWorkflowOperation, ); - return newWorkflow.id; + return newWorkflowCandidate.id; } } diff --git a/examples/bri-3/src/shared/prisma/prisma.service.ts b/examples/bri-3/src/shared/prisma/prisma.service.ts index c19aa9f32..776014623 100644 --- a/examples/bri-3/src/shared/prisma/prisma.service.ts +++ b/examples/bri-3/src/shared/prisma/prisma.service.ts @@ -1,12 +1,20 @@ -import { Injectable, OnModuleInit } from '@nestjs/common'; -import { PrismaClient } from '@prisma/client'; +import { Injectable } from '@nestjs/common'; +import { PrismaClient, PrismaPromise } from '@prisma/client'; @Injectable() -export class PrismaService extends PrismaClient implements OnModuleInit { +export class PrismaService extends PrismaClient { constructor() { super({ log: ['info'] }); } - async onModuleInit() { - await this.$connect(); + + public async executeTransaction(...operations: PrismaPromise[]) { + if (operations.length === 0) return; + + try { + await this.$transaction(operations); + } catch (e) { + // TODO: Add transaction error message + throw e; + } } } From 332edfc912e19fbca22c2d65f37073e4cef8afab Mon Sep 17 00:00:00 2001 From: Kasshern Date: Thu, 11 Jan 2024 10:10:27 -0600 Subject: [PATCH 04/10] Alters prisma service executeTransaction to accept DB operation type and accept and await spread operator operations --- .../bri-3/src/shared/prisma/prisma.service.ts | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/examples/bri-3/src/shared/prisma/prisma.service.ts b/examples/bri-3/src/shared/prisma/prisma.service.ts index 776014623..3ddeb62d8 100644 --- a/examples/bri-3/src/shared/prisma/prisma.service.ts +++ b/examples/bri-3/src/shared/prisma/prisma.service.ts @@ -1,20 +1,23 @@ import { Injectable } from '@nestjs/common'; import { PrismaClient, PrismaPromise } from '@prisma/client'; +type DbOperation = () => Promise | PrismaPromise; + @Injectable() export class PrismaService extends PrismaClient { constructor() { super({ log: ['info'] }); } - public async executeTransaction(...operations: PrismaPromise[]) { - if (operations.length === 0) return; - - try { - await this.$transaction(operations); - } catch (e) { - // TODO: Add transaction error message - throw e; - } + public async executeTransaction( + ...operations: DbOperation[] + ): Promise { + return await this.$transaction(async () => { + const results: any[] = []; + for (const operation of operations) { + results.push(await operation()); + } + return results; + }); } } From daba4404ae2c09dbffdcd12c42840b462810a55d Mon Sep 17 00:00:00 2001 From: Kasshern Date: Thu, 11 Jan 2024 10:12:42 -0600 Subject: [PATCH 05/10] alters workflow storage agent to bind storage functions with prisma storage agents, and make sure of closures in passing arguments to prisma service execute transaction --- .../agents/workflowsStorage.agent.ts | 28 +++++++++++++------ .../createWorkflowCommand.handler.ts | 17 ++++------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts b/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts index 7602520ef..c15c27d22 100644 --- a/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts +++ b/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts @@ -4,15 +4,17 @@ import { Injectable, NotFoundException } from '@nestjs/common'; import { WORKFLOW_NOT_FOUND_ERR_MESSAGE } from '../api/err.messages'; import { Workflow } from '../models/workflow'; import { Workflow as WorkflowModel } from '@prisma/client'; -import { BpiAccount as BpiAccountModel } from '@prisma/client'; import { PrismaService } from '../../../../shared/prisma/prisma.service'; import { PrismaPromise } from '@prisma/client'; +import { BpiAccountStorageAgent } from '../../../identity/bpiAccounts/agents/bpiAccountsStorage.agent'; +import { BpiAccount } from '../../../identity/bpiAccounts/models/bpiAccount'; @Injectable() export class WorkflowStorageAgent { constructor( @InjectMapper() private mapper: Mapper, private readonly prisma: PrismaService, + private readonly bpiAccountStorageAgent: BpiAccountStorageAgent, ) {} async getWorkflowById(id: string): Promise { @@ -107,12 +109,22 @@ export class WorkflowStorageAgent { } async storeWorkflowTransaction( - bpiAccountOperation: PrismaPromise, - workflowOperation: PrismaPromise, - ) { - await this.prisma.executeTransaction( - bpiAccountOperation, - workflowOperation, - ); + bpiAccountCandidate: BpiAccount, + workflowCandidate: Workflow, + ): Promise { + const boundStoreNewWorkflow = this.storeNewWorkflow.bind(this); + const boundStoreNewBpiAccount = + this.bpiAccountStorageAgent.storeNewBpiAccount.bind( + this.bpiAccountStorageAgent, + ); + const results = await this.prisma.executeTransaction(() => { + return boundStoreNewBpiAccount(bpiAccountCandidate).then( + (newBpiAccount: { id: string }) => { + workflowCandidate.bpiAccountId = newBpiAccount.id; + return boundStoreNewWorkflow(workflowCandidate); + }, + ); + }); + return results; } } diff --git a/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts b/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts index 970d0674c..569ffad5c 100644 --- a/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts +++ b/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts @@ -1,6 +1,5 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; import { BpiAccountAgent } from '../../../../identity/bpiAccounts/agents/bpiAccounts.agent'; -import { BpiAccountStorageAgent } from '../../../../identity/bpiAccounts/agents/bpiAccountsStorage.agent'; import { BpiSubjectAccountAgent } from '../../../../identity/bpiSubjectAccounts/agents/bpiSubjectAccounts.agent'; import { WorkgroupAgent } from '../../../../workgroup/workgroups/agents/workgroups.agent'; import { WorkflowAgent } from '../../agents/workflows.agent'; @@ -17,7 +16,6 @@ export class CreateWorkflowCommandHandler private subjectAccountAgent: BpiSubjectAccountAgent, private workgroupAgent: WorkgroupAgent, private storageAgent: WorkflowStorageAgent, - private accountStorageAgent: BpiAccountStorageAgent, ) {} async execute(command: CreateWorkflowCommand) { @@ -47,9 +45,6 @@ export class CreateWorkflowCommandHandler 'sample state object prover system', ); - const storeNewBpiAccountOperation = - this.accountStorageAgent.storeNewBpiAccount(newBpiAccountCandidate); - const newWorkflowCandidate = this.agent.createNewWorkflow( command.name, workstepsToConnect, @@ -57,14 +52,12 @@ export class CreateWorkflowCommandHandler newBpiAccountCandidate, ); - const storeNewWorkflowOperation = - this.storageAgent.storeNewWorkflow(newWorkflowCandidate); - - this.storageAgent.storeWorkflowTransaction( - storeNewBpiAccountOperation, - storeNewWorkflowOperation, + const results = await this.storageAgent.storeWorkflowTransaction( + newBpiAccountCandidate, + newWorkflowCandidate, ); - return newWorkflowCandidate.id; + console.log(results); + return results; } } From e971b4fd700298c95e49a6cb04e3824f57fadb23 Mon Sep 17 00:00:00 2001 From: Kasshern Date: Mon, 13 Nov 2023 11:58:15 -0600 Subject: [PATCH 06/10] Moves prisma instatiation into own module, imports that module into storage agents, deletes storage agent extension of prismaService. --- .../bri/identity/bpiSubjects/agents/bpiSubjectsStorage.agent.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/bri-3/src/bri/identity/bpiSubjects/agents/bpiSubjectsStorage.agent.ts b/examples/bri-3/src/bri/identity/bpiSubjects/agents/bpiSubjectsStorage.agent.ts index 8872813f8..dacf87d8f 100644 --- a/examples/bri-3/src/bri/identity/bpiSubjects/agents/bpiSubjectsStorage.agent.ts +++ b/examples/bri-3/src/bri/identity/bpiSubjects/agents/bpiSubjectsStorage.agent.ts @@ -4,6 +4,7 @@ import { PrismaService } from '../../../../shared/prisma/prisma.service'; import { NOT_FOUND_ERR_MESSAGE } from '../api/err.messages'; import { BpiSubject } from '../models/bpiSubject'; import { BpiSubjectRole, BpiSubjectRoleName } from '../models/bpiSubjectRole'; +import { PrismaService } from '../../../../shared/prisma/prisma.service'; // Repositories are the only places that talk the Prisma language of models. // They are always mapped to and from domain objects so that the business layer of the application From 9b3720144d2718cd68b262bc28079d49b2a0508b Mon Sep 17 00:00:00 2001 From: Kasshern Date: Wed, 29 Nov 2023 15:20:36 -0600 Subject: [PATCH 07/10] feat: creates execute prisma transaction function in prisma service and converts POC prisma actions into prisma promises within respective storage agents --- .../agents/workflowsStorage.agent.ts | 19 +++++++++++++++---- .../createWorkflowCommand.handler.ts | 17 ++++++++++------- .../bri-3/src/shared/prisma/prisma.service.ts | 13 ++++++++++++- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts b/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts index 0cc0215db..7602520ef 100644 --- a/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts +++ b/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts @@ -3,7 +3,10 @@ import { InjectMapper } from '@automapper/nestjs'; import { Injectable, NotFoundException } from '@nestjs/common'; import { WORKFLOW_NOT_FOUND_ERR_MESSAGE } from '../api/err.messages'; import { Workflow } from '../models/workflow'; +import { Workflow as WorkflowModel } from '@prisma/client'; +import { BpiAccount as BpiAccountModel } from '@prisma/client'; import { PrismaService } from '../../../../shared/prisma/prisma.service'; +import { PrismaPromise } from '@prisma/client'; @Injectable() export class WorkflowStorageAgent { @@ -49,14 +52,14 @@ export class WorkflowStorageAgent { }); } - async storeNewWorkflow(workflow: Workflow): Promise { + storeNewWorkflow(workflow: Workflow): PrismaPromise { const workstepIds = workflow.worksteps.map((w) => { return { id: w.id, }; }); - const newWorkflowModel = await this.prisma.workflow.create({ + return this.prisma.workflow.create({ data: { id: workflow.id, name: workflow.name, @@ -71,8 +74,6 @@ export class WorkflowStorageAgent { bpiAccount: true, }, }); - - return this.mapper.map(newWorkflowModel, Workflow, Workflow); } async updateWorkflow(workflow: Workflow): Promise { @@ -104,4 +105,14 @@ export class WorkflowStorageAgent { where: { id: workflow.id }, }); } + + async storeWorkflowTransaction( + bpiAccountOperation: PrismaPromise, + workflowOperation: PrismaPromise, + ) { + await this.prisma.executeTransaction( + bpiAccountOperation, + workflowOperation, + ); + } } diff --git a/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts b/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts index 1334ec201..afb7624b8 100644 --- a/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts +++ b/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts @@ -47,21 +47,24 @@ export class CreateWorkflowCommandHandler 'sample state object prover system', ); - const newBpiAccount = await this.accountStorageAgent.storeNewBpiAccount( - newBpiAccountCandidate, - ); + const storeNewBpiAccountOperation = + this.accountStorageAgent.storeNewBpiAccount(newBpiAccountCandidate); const newWorkflowCandidate = this.agent.createNewWorkflow( command.name, workstepsToConnect, command.workgroupId, - newBpiAccount, + newBpiAccountCandidate, ); - const newWorkflow = await this.storageAgent.storeNewWorkflow( - newWorkflowCandidate, + const storeNewWorkflowOperation = + this.storageAgent.storeNewWorkflow(newWorkflowCandidate); + + this.storageAgent.storeWorkflowTransaction( + storeNewBpiAccountOperation, + storeNewWorkflowOperation, ); - return newWorkflow.id; + return newWorkflowCandidate.id; } } diff --git a/examples/bri-3/src/shared/prisma/prisma.service.ts b/examples/bri-3/src/shared/prisma/prisma.service.ts index 83cc7e1c8..776014623 100644 --- a/examples/bri-3/src/shared/prisma/prisma.service.ts +++ b/examples/bri-3/src/shared/prisma/prisma.service.ts @@ -1,9 +1,20 @@ import { Injectable } from '@nestjs/common'; -import { PrismaClient } from '@prisma/client'; +import { PrismaClient, PrismaPromise } from '@prisma/client'; @Injectable() export class PrismaService extends PrismaClient { constructor() { super({ log: ['info'] }); } + + public async executeTransaction(...operations: PrismaPromise[]) { + if (operations.length === 0) return; + + try { + await this.$transaction(operations); + } catch (e) { + // TODO: Add transaction error message + throw e; + } + } } From 2dfbd03a2b2751dc2a8789a34e3feda0a7db2818 Mon Sep 17 00:00:00 2001 From: Kasshern Date: Thu, 11 Jan 2024 10:10:27 -0600 Subject: [PATCH 08/10] Alters prisma service executeTransaction to accept DB operation type and accept and await spread operator operations --- .../bri-3/src/shared/prisma/prisma.service.ts | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/examples/bri-3/src/shared/prisma/prisma.service.ts b/examples/bri-3/src/shared/prisma/prisma.service.ts index 776014623..3ddeb62d8 100644 --- a/examples/bri-3/src/shared/prisma/prisma.service.ts +++ b/examples/bri-3/src/shared/prisma/prisma.service.ts @@ -1,20 +1,23 @@ import { Injectable } from '@nestjs/common'; import { PrismaClient, PrismaPromise } from '@prisma/client'; +type DbOperation = () => Promise | PrismaPromise; + @Injectable() export class PrismaService extends PrismaClient { constructor() { super({ log: ['info'] }); } - public async executeTransaction(...operations: PrismaPromise[]) { - if (operations.length === 0) return; - - try { - await this.$transaction(operations); - } catch (e) { - // TODO: Add transaction error message - throw e; - } + public async executeTransaction( + ...operations: DbOperation[] + ): Promise { + return await this.$transaction(async () => { + const results: any[] = []; + for (const operation of operations) { + results.push(await operation()); + } + return results; + }); } } From f42a685a82167fa39483eb6169cc09a117ca7581 Mon Sep 17 00:00:00 2001 From: Kasshern Date: Thu, 11 Jan 2024 10:12:42 -0600 Subject: [PATCH 09/10] alters workflow storage agent to bind storage functions with prisma storage agents, and make sure of closures in passing arguments to prisma service execute transaction --- .../agents/workflowsStorage.agent.ts | 28 +++++++++++++------ .../createWorkflowCommand.handler.ts | 16 ++++------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts b/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts index 7602520ef..c15c27d22 100644 --- a/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts +++ b/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts @@ -4,15 +4,17 @@ import { Injectable, NotFoundException } from '@nestjs/common'; import { WORKFLOW_NOT_FOUND_ERR_MESSAGE } from '../api/err.messages'; import { Workflow } from '../models/workflow'; import { Workflow as WorkflowModel } from '@prisma/client'; -import { BpiAccount as BpiAccountModel } from '@prisma/client'; import { PrismaService } from '../../../../shared/prisma/prisma.service'; import { PrismaPromise } from '@prisma/client'; +import { BpiAccountStorageAgent } from '../../../identity/bpiAccounts/agents/bpiAccountsStorage.agent'; +import { BpiAccount } from '../../../identity/bpiAccounts/models/bpiAccount'; @Injectable() export class WorkflowStorageAgent { constructor( @InjectMapper() private mapper: Mapper, private readonly prisma: PrismaService, + private readonly bpiAccountStorageAgent: BpiAccountStorageAgent, ) {} async getWorkflowById(id: string): Promise { @@ -107,12 +109,22 @@ export class WorkflowStorageAgent { } async storeWorkflowTransaction( - bpiAccountOperation: PrismaPromise, - workflowOperation: PrismaPromise, - ) { - await this.prisma.executeTransaction( - bpiAccountOperation, - workflowOperation, - ); + bpiAccountCandidate: BpiAccount, + workflowCandidate: Workflow, + ): Promise { + const boundStoreNewWorkflow = this.storeNewWorkflow.bind(this); + const boundStoreNewBpiAccount = + this.bpiAccountStorageAgent.storeNewBpiAccount.bind( + this.bpiAccountStorageAgent, + ); + const results = await this.prisma.executeTransaction(() => { + return boundStoreNewBpiAccount(bpiAccountCandidate).then( + (newBpiAccount: { id: string }) => { + workflowCandidate.bpiAccountId = newBpiAccount.id; + return boundStoreNewWorkflow(workflowCandidate); + }, + ); + }); + return results; } } diff --git a/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts b/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts index afb7624b8..f0c434397 100644 --- a/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts +++ b/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts @@ -17,7 +17,6 @@ export class CreateWorkflowCommandHandler private subjectAccountAgent: BpiSubjectAccountAgent, private workgroupAgent: WorkgroupAgent, private storageAgent: WorkflowStorageAgent, - private accountStorageAgent: BpiAccountStorageAgent, ) {} async execute(command: CreateWorkflowCommand) { @@ -47,9 +46,6 @@ export class CreateWorkflowCommandHandler 'sample state object prover system', ); - const storeNewBpiAccountOperation = - this.accountStorageAgent.storeNewBpiAccount(newBpiAccountCandidate); - const newWorkflowCandidate = this.agent.createNewWorkflow( command.name, workstepsToConnect, @@ -57,14 +53,12 @@ export class CreateWorkflowCommandHandler newBpiAccountCandidate, ); - const storeNewWorkflowOperation = - this.storageAgent.storeNewWorkflow(newWorkflowCandidate); - - this.storageAgent.storeWorkflowTransaction( - storeNewBpiAccountOperation, - storeNewWorkflowOperation, + const results = await this.storageAgent.storeWorkflowTransaction( + newBpiAccountCandidate, + newWorkflowCandidate, ); - return newWorkflowCandidate.id; + console.log(results); + return results; } } From 03aebdc302cb475a3c9d68b1a111b59e56802bf6 Mon Sep 17 00:00:00 2001 From: Kasshern Date: Thu, 11 Jan 2024 10:47:27 -0600 Subject: [PATCH 10/10] corrects imports in workflowstoraagent and removes log in createworkflowcommandhandler --- .../bri/workgroup/workflows/agents/workflowsStorage.agent.ts | 5 ++--- .../createWorkflow/createWorkflowCommand.handler.ts | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts b/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts index c15c27d22..cef36d259 100644 --- a/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts +++ b/examples/bri-3/src/bri/workgroup/workflows/agents/workflowsStorage.agent.ts @@ -3,11 +3,10 @@ import { InjectMapper } from '@automapper/nestjs'; import { Injectable, NotFoundException } from '@nestjs/common'; import { WORKFLOW_NOT_FOUND_ERR_MESSAGE } from '../api/err.messages'; import { Workflow } from '../models/workflow'; -import { Workflow as WorkflowModel } from '@prisma/client'; +import { BpiAccount, Workflow as WorkflowModel } from '@prisma/client'; import { PrismaService } from '../../../../shared/prisma/prisma.service'; import { PrismaPromise } from '@prisma/client'; -import { BpiAccountStorageAgent } from '../../../identity/bpiAccounts/agents/bpiAccountsStorage.agent'; -import { BpiAccount } from '../../../identity/bpiAccounts/models/bpiAccount'; +import { BpiAccountStorageAgent } from '../../../state/bpiAccounts/agents/bpiAccountsStorage.agent'; @Injectable() export class WorkflowStorageAgent { diff --git a/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts b/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts index 1fb5c3cec..e89ecf9d4 100644 --- a/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts +++ b/examples/bri-3/src/bri/workgroup/workflows/capabilities/createWorkflow/createWorkflowCommand.handler.ts @@ -57,7 +57,6 @@ export class CreateWorkflowCommandHandler newWorkflowCandidate, ); - console.log(results); return results; } }