Skip to content

Commit

Permalink
Check for if workflow's parent project has authors and reviewers
Browse files Browse the repository at this point in the history
  • Loading branch information
FyreByrd committed Oct 10, 2024
1 parent d0f055c commit 42a92a9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
4 changes: 4 additions & 0 deletions source/SIL.AppBuilder.Portal/common/public/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export type WorkflowContext = {
environment: { [key: string]: any };
productType: ProductType;
productId: string;
hasAuthors: boolean;
hasReviewers: boolean;
};

export type WorkflowConfig = {
Expand All @@ -94,6 +96,8 @@ export type WorkflowConfig = {

export type WorkflowInput = WorkflowConfig & {
productId: string;
hasAuthors: boolean;
hasReviewers: boolean;
};

// TODO: Just put this info directly in the database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ export const DefaultWorkflow = setup({
(params.URFeatures ? context.URFeatures.filter((urf) => params.URFeatures.includes(urf)).length > 0 : true)
);
},
// TODO: write actual guards. cannot be async, which means no checking DB
hasAuthors: ({ context }) => {
return true;
return context.hasAuthors;
},
hasReviewers: ({ context }) => {
return true;
return context.hasReviewers;
}
}
}).createMachine({
Expand All @@ -72,7 +71,9 @@ export const DefaultWorkflow = setup({
environment: {},
productType: input.productType,
URFeatures: input.URFeatures,
productId: input.productId
productId: input.productId,
hasAuthors: input.hasAuthors,
hasReviewers: input.hasReviewers
}),
states: {
Start: {
Expand Down
39 changes: 37 additions & 2 deletions source/SIL.AppBuilder.Portal/common/workflow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,18 @@ export class Workflow {
public static async create(productId: string, input: WorkflowConfig): Promise<Workflow> {
const flow = new Workflow(productId, input);

const check = await flow.checkAuthorsAndReviewers();

flow.flow = createActor(DefaultWorkflow, {
inspect: (e) => {
if (e.type === '@xstate.snapshot') flow.inspect(e);
},
input: { productId, ...input }
input: {
productId,
hasAuthors: check._count.Authors > 0,
hasReviewers: check._count.Authors > 0,
...input
}
});

flow.flow.start();
Expand All @@ -75,12 +82,18 @@ export class Workflow {
public static async restore(productId: string): Promise<Workflow> {
const snap = await Workflow.getSnapshot(productId);
const flow = new Workflow(snap.context.productId, snap.context);
const check = await flow.checkAuthorsAndReviewers();
flow.flow = createActor(DefaultWorkflow, {
snapshot: snap ? DefaultWorkflow.resolveState(snap) : undefined,
inspect: (e) => {
if (e.type === '@xstate.snapshot') flow.inspect(e);
},
input: snap.context
input: {
...snap.context,
productId: productId,
hasAuthors: check._count.Authors > 0,
hasReviewers: check._count.Authors > 0
}
});

flow.flow.start();
Expand Down Expand Up @@ -450,4 +463,26 @@ export class Workflow {
?.replace('#' + DefaultWorkflow.id + '.', '') || ''
);
}

private async checkAuthorsAndReviewers() {
return (
await prisma.projects.findMany({
where: {
Products: {
some: {
Id: this.productId
}
}
},
select: {
_count: {
select: {
Authors: true,
Reviewers: true
}
}
}
})
)[0];
}
}

0 comments on commit 42a92a9

Please sign in to comment.