From c3dcbc47a386563bb48fbb56c5d3252d3d54fa29 Mon Sep 17 00:00:00 2001 From: dat Date: Wed, 26 Apr 2023 09:10:11 +0700 Subject: [PATCH] Self-service - Launching a bug hunt failing --- .../work-functions/work-factory/work.factory.ts | 5 ++++- .../work-functions/work.functions.ts | 15 ++++++++++++++- src-ts/utils/string.ts | 9 +++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src-ts/utils/string.ts diff --git a/src-ts/tools/work/work-lib/work-provider/work-functions/work-factory/work.factory.ts b/src-ts/tools/work/work-lib/work-provider/work-functions/work-factory/work.factory.ts index ff0b2128e..558c070ac 100644 --- a/src-ts/tools/work/work-lib/work-provider/work-functions/work-factory/work.factory.ts +++ b/src-ts/tools/work/work-lib/work-provider/work-functions/work-factory/work.factory.ts @@ -450,7 +450,10 @@ function checkFormDataOptionEmpty(detail: any): boolean { ) } -function findMetadata(challenge: Challenge, metadataName: ChallengeMetadataName): ChallengeMetadata | undefined { +export function findMetadata( + challenge: Challenge | CreateWorkRequest, + metadataName: ChallengeMetadataName, +): ChallengeMetadata | undefined { return challenge.metadata?.find((item: ChallengeMetadata) => item.name === metadataName) } diff --git a/src-ts/tools/work/work-lib/work-provider/work-functions/work.functions.ts b/src-ts/tools/work/work-lib/work-provider/work-functions/work.functions.ts index 5111589cd..2bab5ba00 100644 --- a/src-ts/tools/work/work-lib/work-provider/work-functions/work.functions.ts +++ b/src-ts/tools/work/work-lib/work-provider/work-functions/work.functions.ts @@ -1,6 +1,7 @@ import { PaymentMethodResult, Stripe, StripeCardNumberElement } from '@stripe/stripe-js' import { FormCard, GenericDataObject, Page, textFormatMoneyLocaleString, UserProfile } from '../../../../../lib' +import { isJsonString } from '../../../../../utils/string' // this has to be imported directly from the file bc the order of operations // that items are loaded in the barrel file this config is empty and throws an error // eslint-disable-next-line ordered-imports/ordered-imports @@ -17,6 +18,8 @@ import { import { ActivateWorkRequest, Challenge, + ChallengeMetadata, + ChallengeMetadataName, CreateWorkRequest, CustomerPayment, CustomerPaymentRequest, @@ -41,11 +44,21 @@ import { WorkTypeConfig, WorkTypeConfigs, } from './work-store' +import { findMetadata } from './work-factory/work.factory' export async function createAsync(type: WorkType): Promise { const workConfig: WorkTypeConfig = WorkTypeConfigs[type] const body: CreateWorkRequest = workFactoryBuildCreateReqeuest(workConfig) - return workStoreCreateAsync(body) + const result: Challenge = await workStoreCreateAsync(body) + + const intakeFormResponse: ChallengeMetadata | undefined + = findMetadata(result, ChallengeMetadataName.intakeForm) || undefined + if (!!intakeFormResponse?.value && !isJsonString(intakeFormResponse.value)) { + const bodyIntakeForm: ChallengeMetadata | undefined = findMetadata(body, ChallengeMetadataName.intakeForm) + intakeFormResponse.value = bodyIntakeForm?.value || '{}' + } + + return result } export async function createCustomerPaymentAsync( diff --git a/src-ts/utils/string.ts b/src-ts/utils/string.ts new file mode 100644 index 000000000..5922fc83d --- /dev/null +++ b/src-ts/utils/string.ts @@ -0,0 +1,9 @@ +export function isJsonString(str: string): boolean { + try { + JSON.parse(str) + } catch (e) { + return false + } + + return true +}