Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak committed Dec 20, 2024
1 parent 85b2d25 commit 9be686c
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 0 deletions.
92 changes: 92 additions & 0 deletions dec2024/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
require("isomorphic-fetch");

class Client {
constructor({ hasuraSecret, targetURL }) {
this.secret = hasuraSecret;
this.url = targetURL;
}

async graphQL(query, variables) {
const result = await fetch(this.url, {
method: "POST",
body: JSON.stringify({ query, variables }),
headers: {
"x-hasura-admin-secret": this.secret,
},
});
return result.json();
}

async getQueuedFlow() {
const { data, errors } = await this.graphQL(
`query GetQueuedFlow {
temp_data_migrations_audit(limit: 1, where: {updated: {_eq: false}}) {
id: flow_id
flow: flows {
slug
team {
slug
}
data
publishedFlows: published_flows(limit: 1, order_by: {created_at: desc}) {
id
data
}
}
}
}`
);

if (errors || !data) throw new Error(formatJSON({ data, errors }));
return data.temp_data_migrations_audit[0];
}

async updateQueuedFlow(flowId, liveFlowData) {
const { data, errors } = await this.graphQL(
`mutation UpdateFlow ($flowId: uuid!, $liveData: jsonb!) {
update_flows_by_pk(pk_columns: {id: $flowId}, _set: {data: $liveData}) {
id
}
update_temp_data_migrations_audit_by_pk(pk_columns: {flow_id: $flowId}, _set: {updated: true}) {
flow_id
}
}`,
{
flowId: flowId,
liveData: liveFlowData,
}
);
if (errors || !data) throw new Error(formatJSON({ data, errors }));
return { ...data };
}

async updateQueuedPublishedFlow(flowId, liveFlowData, publishedFlowId, publishedFlowData) {
const { data, errors } = await this.graphQL(
`mutation UpdateFlowAndPublishedFlow ($flowId: uuid!, $liveData: jsonb!, $publishedFlowId: Int!, $publishedData: jsonb!) {
update_flows_by_pk(pk_columns: {id: $flowId}, _set: {data: $liveData}) {
id
}
update_published_flows_by_pk(pk_columns: {id: $publishedFlowId}, _set: {data: $publishedData}) {
id
}
update_temp_data_migrations_audit_by_pk(pk_columns: {flow_id: $flowId}, _set: {updated: true}) {
flow_id
}
}`,
{
flowId: flowId,
liveData: liveFlowData,
publishedFlowId: publishedFlowId,
publishedData: publishedFlowData,
}
);
if (errors || !data) throw new Error(formatJSON({ data, errors }));
return { ...data };
}
}

function formatJSON({ data, errors }) {
return JSON.stringify({ data, errors }, null, 2);
}

module.exports = Client;
94 changes: 94 additions & 0 deletions dec2024/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const chalk = require("chalk");
const Client = require("./client");

const timestamp = `[${new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')}]`;

(async function go() {
const url = {
production: "https://hasura.editor.planx.uk/v1/graphql",
staging: "https://hasura.editor.planx.dev/v1/graphql",
local: "http://localhost:7100/v1/graphql",
};

const hasuraSecret = process.env["HASURA_SECRET"];
const env = process.env["HASURA_ENV"];
console.log(chalk.cyan(`${timestamp} Connecting to Hasura ${env}`));

// Create GraphQL client
const client = new Client({
hasuraSecret,
targetURL: url[env],
});

// Get a flow that hasn't been updated yet and its' published version if applicable
// TODO handle related `lowcal_session.data` when applicable?
const { id, flow } = await client.getQueuedFlow();
const flowSlug = `${flow?.team?.slug}/${flow?.slug}`;

if (id) {
try {
console.log(`${timestamp} Updating flow ${flowSlug}`);
let flowData;
flowData = updateFlowData(flow.data);

if (flow.publishedFlows?.length > 0) {
console.log(`${timestamp} Updating published flow ${flowSlug}`);
let publishedFlowData;
publishedFlowData = updateFlowData(flow.publishedFlows?.[0]?.data);

// Update in a single mutation block for postgres transaction-like rollback behavior on error
const publishedFlowResponse = await client.updateQueuedPublishedFlow(id, liveFlowData, flow.publishedFlows?.[0]?.id, publishedFlowData);
if (
publishedFlowResponse?.update_flows_by_pk?.id &&
publishedFlowResponse?.update_published_flows_by_pk?.id &&
publishedFlowResponse?.update_temp_data_migrations_audit_by_pk?.flow_id
) {
console.log(`${timestamp} Successfully updated ${flowSlug} - live flow, published flow, and audit table`);
}
} else {
const flowResponse = await client.updateQueuedFlow(id, flowData);
if (
flowResponse?.update_flows_by_pk?.id &&
flowResponse?.update_temp_data_migrations_audit_by_pk?.flow_id
) {
console.log(`${timestamp} Successfully updated ${flowSlug} - live flow and audit table only, not published`);
}
}
} catch (error) {
console.log(chalk.red(`Error: ${error}`));
}
}
})();

// Follows details outlined in gsheet here https://docs.google.com/spreadsheets/d/1Vtxp5BLweDPDooQoNhgOCYjCIBPRYIcOuyArGJRqOkI/edit?gid=0#gid=0
const updateFlowData = (flowData) => {
let newFlowData = flowData;
Object.entries(flowData).forEach(([nodeId, nodeData]) => {
// Project type schema
if (nodeData?.["data"]?.["val"] === "changeofUse.annexe") {
newFlowData[nodeId]["data"]["val"] = "changeOfUse.annexe";
console.log(`${timestamp} Updated project type value`);
}

// About the property
if (nodeData?.["type"] === 12) {
newFlowData[nodeId]["data"] = defaultPropertyInformationNodeData;
console.log(`${timestamp} Updated PropertyInformation content`);
}

// Calculate prop
if (nodeData?.["type"] === 700 && nodeData?.["data"]?.["output"]) {
newFlowData[nodeId]["data"]["fn"] = nodeData["data"]["output"];
delete newFlowData[nodeId]["data"]["output"];
console.log(`${timestamp} Updated Calculate prop`);
}
});

return newFlowData;
}

const defaultPropertyInformationNodeData = {
"title": "About the property",
"description": "<p>This is the information we currently have about the property.</p><p>The blue line shows the <strong>outline</strong> of the property (known as the title boundary). If this looks incorrect, go back a step and <strong>check you have selected the correct address</strong>.</p><p>We use this outline to create the site boundary where the project will take place. If your project covers a different area, you can change or redraw the site boundary on the next page.</p>",
"showPropertyTypeOverride": true
};

0 comments on commit 9be686c

Please sign in to comment.