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

fix: checklist agg, add some tests #82

Merged
merged 1 commit into from
Dec 3, 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
10 changes: 10 additions & 0 deletions schema/application/resolvers/ChecklistAggregate.test.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
query TestChecklistAgg(
$parent: ID!
$assignedTo: [ID!]!
$dueOnInput: TemporalRangeInput!
) {
checklistAgg {
assignedTo(parent: $parent, assignees: $assignedTo)
dueOn(parent: $parent, input: $dueOnInput)
}
}
52 changes: 52 additions & 0 deletions schema/application/resolvers/ChecklistAggregate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { describe, expect, test } from "bun:test";
import { resolvers, typeDefs } from "@/schema";
import { encodeGlobalId } from "@/schema/system";
import { execute } from "@/test/prelude";
import { makeExecutableSchema } from "@graphql-tools/schema";
import { TestChecklistAggDocument } from "./ChecklistAggregate.test.generated";

const schema = makeExecutableSchema({ resolvers, typeDefs });

const ASSIGNEE = encodeGlobalId({
type: "worker",
id: "worker-instance_cbcb5607-373a-45df-aae0-01bdddc744e4",
});
const CUSTOMER = encodeGlobalId({
type: "organization",
id: "customer_1b2d6c60-8678-45ad-b30d-a10323c2c441",
});
const TEMPLATE = encodeGlobalId({
type: "worktemplate",
id: "a2b2004d-49a6-4fa2-b8e5-d86df0041fdd",
});
const DUE_ON_BEFORE = "1733251472196"; // Date.now() circa Tue Dec 03 2024 10:45

describe.skipIf(!!process.env.CI)("ChecklistAggregate", () => {
test("when parent is customer", async () => {
const result = await execute(schema, TestChecklistAggDocument, {
parent: CUSTOMER,
assignedTo: [ASSIGNEE],
dueOnInput: {
before: {
instant: DUE_ON_BEFORE,
},
},
});
expect(result.errors).toBeFalsy();
expect(result).toMatchSnapshot();
});

test("when parent is template", async () => {
const result = await execute(schema, TestChecklistAggDocument, {
parent: TEMPLATE,
assignedTo: [ASSIGNEE],
dueOnInput: {
before: {
instant: DUE_ON_BEFORE,
},
},
});
expect(result.errors).toBeFalsy();
expect(result).toMatchSnapshot();
});
});
162 changes: 109 additions & 53 deletions schema/application/resolvers/ChecklistAggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,70 @@ import type { ChecklistAggregateResolvers } from "@/schema";
import { decodeGlobalId } from "@/schema/system";
import { map } from "@/util";
import { Temporal } from "@js-temporal/polyfill";
import { match } from "ts-pattern";

export const ChecklistAggregate: ChecklistAggregateResolvers = {
async assignedTo(_, args) {
const { type, id } = decodeGlobalId(args.parent);
// TODO: switch on parent type

const assignees = args.assignees.map(e => decodeGlobalId(e).id);
const [{ count }] = await sql<[{ count: number }]>`
SELECT count(*)
FROM public.workresultinstance
INNER JOIN public.workresult
ON
workresultinstanceworkresultid = workresultid
AND workresultentitytypeid = 850
AND workresultisprimary
WHERE
workresultinstancecustomerid IN (
SELECT customerid
FROM public.customer
WHERE customeruuid = ${id}
)
AND workresultinstancevalue IN (
SELECT workerinstanceid::text
FROM public.workerinstance
WHERE id IN ${sql(assignees)}
)
`;

const [{ count }] = await match(type)
.with(
"organization",
() => sql<[{ count: number }]>`
SELECT count(*)
FROM public.workresultinstance AS wri
INNER JOIN public.workresult AS wr
ON
wri.workresultinstanceworkresultid = wr.workresultid
AND wr.workresultentitytypeid = 850
AND wr.workresultisprimary
INNER JOIN public.worktemplatetype AS wtt
ON wr.workresultworktemplateid = wtt.worktemplatetypeworktemplateid
INNER JOIN public.systag AS s
ON wtt.worktemplatetypesystaguuid = s.systaguuid
WHERE
s.systagtype = 'Checklist'
AND workresultinstancecustomerid IN (
SELECT c.customerid
FROM public.customer AS c
WHERE c.customeruuid = ${id}
)
AND workresultinstancevalue IN (
SELECT w.workerinstanceid::text
FROM public.workerinstance AS w
WHERE w.workerinstanceuuid IN ${sql(assignees)}
);
`,
)
.with(
"worktemplate",
() => sql<[{ count: number }]>`
SELECT count(*)
FROM public.workresultinstance AS wri
INNER JOIN public.workresult AS wr
ON
wri.workresultinstanceworkresultid = wr.workresultid
AND wr.workresultentitytypeid = 850
AND wr.workresultisprimary
INNER JOIN public.worktemplate AS wt
ON wr.workresultworktemplateid = wt.worktemplateid
WHERE
wt.id = ${id}
AND workresultinstancevalue IN (
SELECT w.workerinstanceid::text
FROM public.workerinstance AS w
WHERE w.workerinstanceuuid IN ${sql(assignees)}
);
`,
)
.otherwise(() => [{ count: 0 }]);

return count;
},
async dueOn(_, args) {
const { type, id } = decodeGlobalId(args.parent);
console.log("parent", { type, id });
// TODO: switch on parent type

const before = map(args.input.before, input => {
switch (true) {
Expand All @@ -48,10 +78,10 @@ export const ChecklistAggregate: ChecklistAggregateResolvers = {
).toZonedDateTimeISO(input.zdt.timeZone);
default: {
const _: never = input;
throw "invariant violated";
return null;
}
}
});
})?.toString({ calendarName: "never", timeZoneName: "never" });

const after = map(args.input.after, input => {
switch (true) {
Expand All @@ -63,39 +93,65 @@ export const ChecklistAggregate: ChecklistAggregateResolvers = {
).toZonedDateTimeISO(input.zdt.timeZone);
default: {
const _: never = input;
throw "invariant violated";
return null;
}
}
});
})?.toString({ calendarName: "never", timeZoneName: "never" });

if (!after && !before) return 0;

const [{ count }] = await sql<[{ count: number }]>`
SELECT count(*)
FROM public.workinstance
WHERE
workinstancecustomerid IN (
SELECT customerid
FROM public.customer
WHERE customeruuid = ${id}
)
AND ${join(
[
sql`workinstancetargetstartdate IS NOT null`,
...(after
? [
sql`workinstancetargetstartdate > ${after.toString({ calendarName: "never", timeZoneName: "never" })}`,
]
: []),
...(before
? [
sql`workinstancetargetstartdate < ${before.toString({ calendarName: "never", timeZoneName: "never" })}`,
]
: []),
],
sql`AND`,
)}
`;
const [{ count }] = await match(type)
.with(
"organization",
() => sql<[{ count: number }]>`
SELECT count(*)
FROM public.workinstance
WHERE
workinstancecustomerid IN (
SELECT customerid
FROM public.customer
WHERE customeruuid = ${id}
)
AND ${join(
[
sql`workinstancetargetstartdate IS NOT null`,
...(after
? [sql`workinstancetargetstartdate > ${after}`]
: []),
...(before
? [sql`workinstancetargetstartdate < ${before}`]
: []),
],
sql`AND`,
)}
`,
)
.with(
"worktemplate",
() => sql<[{ count: number }]>`
SELECT count(*)
FROM public.workinstance
WHERE
workinstanceworktemplateid IN (
SELECT wt.worktemplateid
FROM public.worktemplate AS wt
WHERE wt.id = ${id}
)
AND ${join(
[
sql`workinstancetargetstartdate IS NOT null`,
...(after
? [sql`workinstancetargetstartdate > ${after}`]
: []),
...(before
? [sql`workinstancetargetstartdate < ${before}`]
: []),
],
sql`AND`,
)}
`,
)
.otherwise(() => [{ count: 0 }]);

return count;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Bun Snapshot v1, https://goo.gl/fbAQLP

exports[`ChecklistAggregate when parent is customer 1`] = `
{
"data": {
"checklistAgg": {
"__typename": "ChecklistAggregate",
"assignedTo": 3,
"dueOn": 25,
},
},
}
`;

exports[`ChecklistAggregate when parent is template 1`] = `
{
"data": {
"checklistAgg": {
"__typename": "ChecklistAggregate",
"assignedTo": 1,
"dueOn": 2,
},
},
}
`;
Loading