Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Prisma Nested Write - Workflow Creation #787

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PrismaMapper } from '../../../../shared/prisma/prisma.mapper';
import { PrismaService } from '../../../../shared/prisma/prisma.service';
import { WORKFLOW_NOT_FOUND_ERR_MESSAGE } from '../api/err.messages';
import { Workflow } from '../models/workflow';
import MerkleTree from 'merkletreejs';

@Injectable()
export class WorkflowStorageAgent {
Expand Down Expand Up @@ -55,23 +56,53 @@ export class WorkflowStorageAgent {
};
});

const newWorkflowModel = await this.prisma.workflow.create({
const connectedOwnerBpiAccounts =
workflow.bpiAccount.ownerBpiSubjectAccounts.map((o) => {
return {
id: o.id,
};
});

const newBpiAccountModel = await this.prisma.bpiAccount.create({
data: {
id: workflow.id,
name: workflow.name,
worksteps: {
connect: workstepIds,
nonce: workflow.bpiAccount.nonce,
ownerBpiSubjectAccounts: {
connect: connectedOwnerBpiAccounts,
},
authorizationCondition: workflow.bpiAccount.authorizationCondition,
stateObjectProverSystem: workflow.bpiAccount.stateObjectProverSystem,
stateTree: {
create: {
id: workflow.bpiAccount.stateTreeId,
hashAlgName: workflow.bpiAccount.stateTree.hashAlgName,
tree: MerkleTree.marshalTree(workflow.bpiAccount.stateTree.tree),
},
},
historyTree: {
create: {
id: workflow.bpiAccount.historyTreeId,
hashAlgName: workflow.bpiAccount.historyTree.hashAlgName,
tree: MerkleTree.marshalTree(workflow.bpiAccount.historyTree.tree),
},
},
Workflow: {
create: [
{
name: workflow.name,
worksteps: {
connect: workstepIds,
},
workgroupId: workflow.workgroupId,
},
],
},
workgroupId: workflow.workgroupId,
bpiAccountId: workflow.bpiAccountId,
},
include: {
worksteps: true,
bpiAccount: true,
Workflow: true,
},
});

return this.mapper.map(newWorkflowModel, Workflow);
return this.mapper.map(newBpiAccountModel.Workflow[0], Workflow);
}

async updateWorkflow(workflow: Workflow): Promise<Workflow> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,11 @@ export class CreateWorkflowCommandHandler
'sample state object prover system',
);

const newBpiAccount = await this.accountStorageAgent.storeNewBpiAccount(
newBpiAccountCandidate,
);

const newWorkflowCandidate = this.agent.createNewWorkflow(
command.name,
workstepsToConnect,
command.workgroupId,
newBpiAccount,
newBpiAccountCandidate,
);

const newWorkflow = await this.storageAgent.storeNewWorkflow(
Expand Down
Loading