Skip to content

Commit b2e18ca

Browse files
committed
chore: [AB#12988] consolidate webflow sync helpers & helpers2
1 parent 91a2c66 commit b2e18ca

File tree

6 files changed

+32
-28
lines changed

6 files changed

+32
-28
lines changed

web/src/scripts/webflow/fundingSync.mjs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
contentToStrings,
88
getHtml,
99
resolveApiPromises,
10+
wait,
1011
} from "./helpers.mjs";
11-
import { wait } from "./helpers2.mjs";
1212
import { createItem, deleteItem, getAllItems, getCollection, modifyItem } from "./methods.mjs";
1313
import { allIndustryId, getCurrentSectors, syncSectors } from "./sectorSync.mjs";
1414
import { certificationCollectionId, fundingCollectionId } from "./webflowIds.mjs";
@@ -184,7 +184,7 @@ const getFundingFromMd = (i, sectors) => {
184184
})?.id;
185185
if (fundingType === undefined) {
186186
throw new Error(
187-
`Funding Types for funding type ${i.fundingType} are mis-matched in ${i.id}. Please check with Webflow.`
187+
`Funding Types for funding type ${i.fundingType} are mis-matched in ${i.id}. Please check with Webflow.`,
188188
);
189189
}
190190
const agency = agencyMap[i.agency[0]]?.id;
@@ -193,15 +193,15 @@ const getFundingFromMd = (i, sectors) => {
193193
You will have to create the agency in Webflow, then call the collection details endpoint
194194
with the fundingCollectionId to get the ID of the option, then adding it to agencyMap. */
195195
throw new Error(
196-
`Agency Types for agency ${i.agency[0]} are mis-matched in ${i.id}. Please check with Webflow.`
196+
`Agency Types for agency ${i.agency[0]} are mis-matched in ${i.id}. Please check with Webflow.`,
197197
);
198198
}
199199
const status = fundingStatusMap.find((v) => {
200200
return v.slug === i.status;
201201
})?.id;
202202
if (status === undefined) {
203203
throw new Error(
204-
`Funding Status Types for funding status type ${i.status} are mis-matched in ${i.id}. Please check with Webflow.`
204+
`Funding Status Types for funding status type ${i.status} are mis-matched in ${i.id}. Please check with Webflow.`,
205205
);
206206
}
207207

@@ -210,7 +210,7 @@ const getFundingFromMd = (i, sectors) => {
210210
(cert) =>
211211
fundingCertificationsMap.find((v) => {
212212
return v.slug === cert;
213-
})?.id
213+
})?.id,
214214
) ?? [];
215215

216216
return {
@@ -225,7 +225,8 @@ const getFundingFromMd = (i, sectors) => {
225225
"last-updated": new Date(Date.now()).toISOString(),
226226
"funding-status": status,
227227
"funding-type": fundingType,
228-
"industry-reference": industryReferenceArray.length > 0 ? industryReferenceArray : [allIndustryId],
228+
"industry-reference":
229+
industryReferenceArray.length > 0 ? industryReferenceArray : [allIndustryId],
229230
};
230231
};
231232

@@ -246,7 +247,7 @@ const getNewFundings = async () => {
246247
const currentIdArray = new Set(
247248
current.map((sec) => {
248249
return sec.fieldData.slug;
249-
})
250+
}),
250251
);
251252
return getFilteredFundings()
252253
.filter((i) => {
@@ -292,7 +293,7 @@ const updateFundings = async () => {
292293
fundings.find((i) => {
293294
return i.id === item.fieldData.slug;
294295
}),
295-
sectors
296+
sectors,
296297
);
297298
console.info(`Attempting to modify ${funding.slug}`);
298299
try {

web/src/scripts/webflow/helpers.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import remarkGfm from "remark-gfm";
66
import remarkParse from "remark-parse";
77
import remarkRehype from "remark-rehype";
88
import { unified } from "unified";
9-
import { wait } from "./helpers2.mjs";
109

1110
const RATE_LIMIT_WAIT_SECONDS = 20;
1211

@@ -115,3 +114,9 @@ export const getHtml = (arrayOfStrings, start, stop) => {
115114
.join(" ")
116115
.trim();
117116
};
117+
118+
export const wait = (milliseconds = 10000) => {
119+
return new Promise((resolve) => {
120+
return setTimeout(resolve, milliseconds);
121+
});
122+
};

web/src/scripts/webflow/helpers2.mjs

Lines changed: 0 additions & 5 deletions
This file was deleted.

web/src/scripts/webflow/licenseSync.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import {
1717
contentToStrings,
1818
getHtml,
1919
resolveApiPromises,
20+
wait,
2021
} from "./helpers.mjs";
21-
import { wait } from "./helpers2.mjs";
2222
import { LicenseClassificationLookup } from "./licenseClassifications.mjs";
2323
import { createItem, deleteItem, getAllItems, modifyItem } from "./methods.mjs";
2424
import { licenseCollectionId } from "./webflowIds.mjs";

web/src/scripts/webflow/sectorSync.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import fs from "fs";
44
import orderBy from "lodash";
55
import path from "path";
66
import { fileURLToPath } from "url";
7-
import { wait } from "./helpers2.mjs";
7+
import { wait } from "./helpers.mjs";
88
import { createItem, deleteItem, getAllItems, modifyItem } from "./methods.mjs";
99
import { allIndustryId, sectorCollectionId } from "./webflowIds.mjs";
1010

1111
const sectorDir = path.resolve(
12-
`${path.dirname(fileURLToPath(import.meta.url))}/../../../../content/src/mappings`
12+
`${path.dirname(fileURLToPath(import.meta.url))}/../../../../content/src/mappings`,
1313
);
1414
const getSectors = () => {
1515
return JSON.parse(fs.readFileSync(path.join(sectorDir, "sectors.json"), "utf8")).arrayOfSectors;
@@ -24,7 +24,7 @@ const getOverlappingSectorsFunc = (currentSectors) => {
2424
return new Set(
2525
getSectors().map((item) => {
2626
return item.id;
27-
})
27+
}),
2828
).has(item.fieldData.slug);
2929
});
3030
};
@@ -37,7 +37,7 @@ const getUpdatedSectors = async () => {
3737
const sectorNames = new Set(
3838
getSectors().map((item) => {
3939
return item.name;
40-
})
40+
}),
4141
);
4242
const overlappingSectors = await getOverlappingSectors();
4343

web/src/scripts/webflow/webflowSync.test.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
updateFundings,
1414
} from "./fundingSync.mjs";
1515
import { catchRateLimitErrorAndRetry } from "./helpers.mjs";
16-
import { wait } from "./helpers2.mjs";
1716
import {
1817
allIndustryId,
1918
createNewSectors,
@@ -268,12 +267,6 @@ jest.mock("fs", () => {
268267
};
269268
});
270269

271-
jest.mock("./helpers2.mjs", () => {
272-
return {
273-
wait: jest.fn(),
274-
};
275-
});
276-
277270
describe("webflow syncing", () => {
278271
const dateNow = Date.now();
279272
const currentDate = new Date(dateNow);
@@ -801,15 +794,25 @@ describe("webflow syncing", () => {
801794

802795
beforeEach(() => {
803796
mockRetryFunc = jest.fn();
797+
jest.useRealTimers();
804798
});
805799

806800
it("should retry function after waiting if rate limit error occurs", async () => {
801+
jest.useFakeTimers();
802+
803+
const setTimeoutSpy = jest.spyOn(globalThis, "setTimeout");
807804
const error = { response: { status: 429 } };
808805

809-
await catchRateLimitErrorAndRetry(error, mockRetryFunc, "arg1", "arg2");
806+
catchRateLimitErrorAndRetry(error, mockRetryFunc, "arg1", "arg2");
807+
await jest.advanceTimersByTimeAsync(65000);
808+
809+
expect(setTimeoutSpy).toHaveBeenCalled();
810+
const lastCall = setTimeoutSpy.mock.calls.at(-1);
811+
expect(lastCall[1]).toBe(65000);
810812

811813
expect(mockRetryFunc).toHaveBeenCalledWith("arg1", "arg2");
812-
expect(wait).toHaveBeenCalledWith(65000);
814+
815+
setTimeoutSpy.mockRestore();
813816
});
814817

815818
it("should throw error if non-rate limit error occurs", async () => {

0 commit comments

Comments
 (0)