Skip to content

Commit

Permalink
feat: alter createFlow to populate creator_id col
Browse files Browse the repository at this point in the history
  • Loading branch information
RODO94 committed Oct 28, 2024
1 parent 4f7c9c6 commit e657171
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
9 changes: 5 additions & 4 deletions e2e/tests/api-driven/src/flowStatusHistory/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { FlowStatus } from "@opensystemslab/planx-core/types";
import { $admin } from "../client";
import { createTeam } from "../globalHelpers";
import { createTeam, createUser } from "../globalHelpers";
import gql from "graphql-tag";

export const setup = async () => {
const teamId = await createTeam();
const userId = await createUser();

const world = { teamId };
const world = { teamId, userId };

return world;
};
Expand Down Expand Up @@ -34,7 +35,7 @@ export const getFlowStatus = async (flowId: string) => {
}
}
`,
{ flowId },
{ flowId }
);

return status;
Expand Down Expand Up @@ -65,7 +66,7 @@ export const getFlowStatusHistory = async (flowId: string) => {
}
}
`,
{ flowId },
{ flowId }
);

return flowStatusHistory;
Expand Down
27 changes: 15 additions & 12 deletions e2e/tests/api-driven/src/flowStatusHistory/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { $admin } from "../client";

export class CustomWorld extends World {
teamId!: number;
userId!: number;
flowId!: string;
}

Expand All @@ -14,15 +15,17 @@ After("@flow-status-history", async function () {
});

Before<CustomWorld>("@flow-status-history", async function () {
const { teamId } = await setup();
const { teamId, userId } = await setup();
this.teamId = teamId;
this.userId = userId;
});

Given<CustomWorld>("a flow exists", async function () {
const flowId = await createFlow({
teamId: this.teamId,
slug: "test-flow",
name: "Test Flow",
userId: this.userId,
});

assert.ok(flowId, "flowId is not defined");
Expand All @@ -36,7 +39,7 @@ Then("the status of the flow is offline by default", async function () {
assert.equal(
status,
"offline",
`Flow status is ${status} - it should be "offline"`,
`Flow status is ${status} - it should be "offline"`
);
});

Expand All @@ -46,28 +49,28 @@ Then("a flow_status_history record is created", async function () {
assert.notEqual(
flowStatusHistory.length,
0,
"No records found for flow_status_history",
"No records found for flow_status_history"
);
assert.equal(
flowStatusHistory.length,
1,
"Multiple records found for flow_status_history",
"Multiple records found for flow_status_history"
);
assert.ok(flowStatusHistory[0], "flow_status_history record not created");
assert.equal(
flowStatusHistory[0].status,
"offline",
`Flow status is ${flowStatusHistory[0].status} - it should be "offline"`,
`Flow status is ${flowStatusHistory[0].status} - it should be "offline"`
);
assert.notEqual(
flowStatusHistory[0].eventStart,
null,
"Event start should be set on INSERT",
"Event start should be set on INSERT"
);
assert.equal(
flowStatusHistory[0].eventEnd,
null,
"Event end should not be set on INSERT",
"Event end should not be set on INSERT"
);
});

Expand All @@ -80,15 +83,15 @@ When("the flow status is changed to online", async function () {
assert.equal(
flow.status,
"online",
`Flow status is ${flow.status} - it should be "online`,
`Flow status is ${flow.status} - it should be "online`
);
});

Then("the open flow_status_history record is updated", async function () {
const flowStatusHistory = await getFlowStatusHistory(this.flowId);
assert.ok(
flowStatusHistory[0].eventEnd,
"Event end should be set on update to status column",
"Event end should be set on update to status column"
);
});

Expand All @@ -99,16 +102,16 @@ Then("a new flow_status_history record is created", async function () {
assert.equal(
flowStatusHistory[1].status,
"online",
`Flow status is ${flowStatusHistory[1].status} - it should be "online"`,
`Flow status is ${flowStatusHistory[1].status} - it should be "online"`
);
assert.notEqual(
flowStatusHistory[1].eventStart,
null,
"Event start should be set on INSERT",
"Event start should be set on INSERT"
);
assert.equal(
flowStatusHistory[1].eventEnd,
null,
"Event end should not be set on INSERT",
"Event end should not be set on INSERT"
);
});
6 changes: 4 additions & 2 deletions e2e/tests/api-driven/src/permissions/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const setup = async () => {
teamId: teamId1,
slug: "team-1-flow",
name: "Team 1 Flow",
userId: user1Id,
});

const user2Id = await createUser({
Expand All @@ -46,6 +47,7 @@ export const setup = async () => {
teamId: teamId2,
slug: "team-2-flow",
name: "Team 2 Flow",
userId: user2Id,
});

const world = {
Expand All @@ -72,7 +74,7 @@ export const performGQLQuery = async ({
const client = (await getClient(world.activeUserEmail)).client;
const { result } = await client.request<Record<"result", GQLQueryResult>>(
query,
variables,
variables
);
return result;
};
Expand All @@ -90,7 +92,7 @@ const buildVariables = (query: DocumentNode, world: CustomWorld) => {

Object.keys(world).forEach((key) => {
const isVariableUsedInQuery = definitionNode.variableDefinitions!.find(
(varDef) => varDef.variable.name.value === key,
(varDef) => varDef.variable.name.value === key
);
if (isVariableUsedInQuery) {
variables[key] = world[key];
Expand Down
19 changes: 16 additions & 3 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ export interface EditorStore extends Store.Store {
connectTo: (id: NodeId) => void;
copyFlow: (flowId: string) => Promise<any>;
copyNode: (id: NodeId) => void;
createFlow: (teamId: any, newSlug: any, newName: string) => Promise<string>;
createFlow: (
teamId: any,
newSlug: any,
newName: string,
userId: number,
) => Promise<string>;
deleteFlow: (teamId: number, flowSlug: string) => Promise<object>;
validateAndDiffFlow: (flowId: string) => Promise<any>;
getFlows: (teamId: number) => Promise<any>;
Expand Down Expand Up @@ -237,17 +242,24 @@ export const editorStore: StateCreator<
localStorage.setItem("clipboard", id);
},

createFlow: async (teamId, newSlug, newName) => {
createFlow: async (teamId, newSlug, newName, userId) => {
let response = (await client.mutate({
mutation: gql`
mutation CreateFlow(
$data: jsonb
$slug: String
$teamId: Int
$userId: Int
$name: String
) {
insert_flows_one(
object: { slug: $slug, team_id: $teamId, version: 1, name: $name }
object: {
slug: $slug
team_id: $teamId
version: 1
name: $name
creator_id: $userId
}
) {
id
data
Expand All @@ -258,6 +270,7 @@ export const editorStore: StateCreator<
name: newName,
slug: newSlug,
teamId,
userId,
},
})) as any;

Expand Down
9 changes: 7 additions & 2 deletions editor.planx.uk/src/pages/Team.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,11 @@ const FlowItem: React.FC<FlowItemProps> = ({
};

const Team: React.FC = () => {
const { id: teamId, slug } = useStore((state) => state.getTeam());
const [{ id: teamId, slug }, user] = useStore((state) => [
state.getTeam(),
state.getUser(),
]);

const [flows, setFlows] = useState<any[] | null>(null);
const navigation = useNavigation();

Expand All @@ -303,6 +307,7 @@ const Team: React.FC = () => {
fetchFlows();
}, [fetchFlows]);

console.log(user);
return (
<Container maxWidth="formWrap">
<Box
Expand Down Expand Up @@ -343,7 +348,7 @@ const Team: React.FC = () => {
!duplicateFlowName
? useStore
.getState()
.createFlow(teamId, newFlowSlug, newFlowName)
.createFlow(teamId, newFlowSlug, newFlowName, user.id)
.then((newId: string) => {
navigation.navigate(`/${slug}/${newId}`);
})
Expand Down

0 comments on commit e657171

Please sign in to comment.