Skip to content

Commit 5a13eb5

Browse files
committed
chore: refactor to have update and create logic in backend
1 parent 73112f3 commit 5a13eb5

File tree

8 files changed

+169
-32
lines changed

8 files changed

+169
-32
lines changed

app/pages/applicantportal/form/[id]/rfi/[applicantRfiId].tsx

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ const getApplicantRfiIdQuery = graphql`
4444
id
4545
ccbcNumber
4646
organizationName
47+
applicationFormTemplate9DataByApplicationId(
48+
filter: { archivedAt: { isNull: true } }
49+
) {
50+
nodes {
51+
rowId
52+
}
53+
}
4754
formData {
4855
id
4956
formSchemaId
@@ -76,6 +83,9 @@ const ApplicantRfiPage = ({
7683
const applicationId = router.query.id as string;
7784
const formSchemaId = applicationByRowId?.formData?.formSchemaId;
7885
const ccbcNumber = applicationByRowId?.ccbcNumber;
86+
const applicationFormTemplate9DataId =
87+
applicationByRowId?.applicationFormTemplate9DataByApplicationId?.nodes?.[0]
88+
?.rowId;
7989
const [newFormData, setNewFormData] = useState(formJsonData);
8090
const [hasApplicationFormDataUpdated, setHasApplicationFormDataUpdated] =
8191
useState(false);
@@ -241,14 +251,14 @@ const ApplicantRfiPage = ({
241251
rfiRowId: rfiDataByRowId.rowId,
242252
},
243253
templateNineInput: {
244-
applicationFormTemplate9Data: {
245-
applicationId: Number(applicationId),
246-
jsonData: templateNineData.data,
247-
source: {
248-
source: 'RFI',
249-
uuid: getTemplateNineUUID(),
250-
},
254+
_applicationId: Number(applicationId),
255+
_jsonData: templateNineData.data,
256+
_previousTemplate9Id: applicationFormTemplate9DataId,
257+
_source: {
258+
source: 'RFI',
259+
uuid: getTemplateNineUUID(),
251260
},
261+
_errors: templateNineData.data?.errors,
252262
},
253263
},
254264
onError: (err) => {
@@ -310,14 +320,14 @@ const ApplicantRfiPage = ({
310320
rfiRowId: rfiDataByRowId.rowId,
311321
},
312322
templateNineInput: {
313-
applicationFormTemplate9Data: {
314-
applicationId: Number(applicationId),
315-
jsonData: templateNineData.data,
316-
source: {
317-
source: 'RFI',
318-
uuid: getTemplateNineUUID(),
319-
},
323+
_applicationId: Number(applicationId),
324+
_jsonData: templateNineData.data,
325+
_source: {
326+
source: 'RFI',
327+
uuid: getTemplateNineUUID(),
320328
},
329+
_previousTemplate9Id: applicationFormTemplate9DataId,
330+
_errors: templateNineData.data?.errors,
321331
},
322332
},
323333
onError: (err) => {

app/schema/mutations/application/updateFormRfiAndCreateTemplateNineDataMutation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import useMutationWithErrorMessage from '../useMutationWithErrorMessage';
55
const mutation = graphql`
66
mutation updateFormRfiAndCreateTemplateNineDataMutation(
77
$rfiInput: UpdateRfiInput!
8-
$templateNineInput: CreateApplicationFormTemplate9DataInput!
8+
$templateNineInput: CreateOrUpdateApplicationFormTemplate9DataInput!
99
$formInput: CreateNewFormDataInput!
1010
) {
1111
updateRfi(input: $rfiInput) {
@@ -15,7 +15,7 @@ const mutation = graphql`
1515
id
1616
}
1717
}
18-
createApplicationFormTemplate9Data(input: $templateNineInput) {
18+
createOrUpdateApplicationFormTemplate9Data(input: $templateNineInput) {
1919
applicationFormTemplate9Data {
2020
rowId
2121
applicationId

app/schema/mutations/application/updateRfiAndCreateTemplateNineDataMutation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import useMutationWithErrorMessage from '../useMutationWithErrorMessage';
55
const mutation = graphql`
66
mutation updateRfiAndCreateTemplateNineDataMutation(
77
$rfiInput: UpdateRfiInput!
8-
$templateNineInput: CreateApplicationFormTemplate9DataInput!
8+
$templateNineInput: CreateOrUpdateApplicationFormTemplate9DataInput!
99
) {
1010
updateRfi(input: $rfiInput) {
1111
rfiData {
@@ -14,7 +14,7 @@ const mutation = graphql`
1414
id
1515
}
1616
}
17-
createApplicationFormTemplate9Data(input: $templateNineInput) {
17+
createOrUpdateApplicationFormTemplate9Data(input: $templateNineInput) {
1818
applicationFormTemplate9Data {
1919
rowId
2020
applicationId

app/schema/schema.graphql

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91071,6 +91071,12 @@ type Mutation {
9107191071
"""
9107291072
input: CreateNewFormDataInput!
9107391073
): CreateNewFormDataPayload
91074+
createOrUpdateApplicationFormTemplate9Data(
91075+
"""
91076+
The exclusive input argument for this mutation. An object type, make sure to see documentation for this object’s fields.
91077+
"""
91078+
input: CreateOrUpdateApplicationFormTemplate9DataInput!
91079+
): CreateOrUpdateApplicationFormTemplate9DataPayload
9107491080
createPackage(
9107591081
"""
9107691082
The exclusive input argument for this mutation. An object type, make sure to see documentation for this object’s fields.
@@ -105486,6 +105492,67 @@ input CreateNewFormDataInput {
105486105492
formSchemaId: Int!
105487105493
}
105488105494

105495+
"""
105496+
The output of our `createOrUpdateApplicationFormTemplate9Data` mutation.
105497+
"""
105498+
type CreateOrUpdateApplicationFormTemplate9DataPayload {
105499+
"""
105500+
The exact same `clientMutationId` that was provided in the mutation input,
105501+
unchanged and unused. May be used by a client to track mutations.
105502+
"""
105503+
clientMutationId: String
105504+
applicationFormTemplate9Data: ApplicationFormTemplate9Data
105505+
105506+
"""
105507+
Our root query field type. Allows us to run any query from our mutation payload.
105508+
"""
105509+
query: Query
105510+
105511+
"""
105512+
Reads a single `Application` that is related to this `ApplicationFormTemplate9Data`.
105513+
"""
105514+
applicationByApplicationId: Application
105515+
105516+
"""
105517+
Reads a single `CcbcUser` that is related to this `ApplicationFormTemplate9Data`.
105518+
"""
105519+
ccbcUserByCreatedBy: CcbcUser
105520+
105521+
"""
105522+
Reads a single `CcbcUser` that is related to this `ApplicationFormTemplate9Data`.
105523+
"""
105524+
ccbcUserByUpdatedBy: CcbcUser
105525+
105526+
"""
105527+
Reads a single `CcbcUser` that is related to this `ApplicationFormTemplate9Data`.
105528+
"""
105529+
ccbcUserByArchivedBy: CcbcUser
105530+
105531+
"""
105532+
An edge for our `ApplicationFormTemplate9Data`. May be used by Relay 1.
105533+
"""
105534+
applicationFormTemplate9DataEdge(
105535+
"""The method to use when ordering `ApplicationFormTemplate9Data`."""
105536+
orderBy: [ApplicationFormTemplate9DataOrderBy!] = [PRIMARY_KEY_ASC]
105537+
): ApplicationFormTemplate9DataEdge
105538+
}
105539+
105540+
"""
105541+
All input for the `createOrUpdateApplicationFormTemplate9Data` mutation.
105542+
"""
105543+
input CreateOrUpdateApplicationFormTemplate9DataInput {
105544+
"""
105545+
An arbitrary string value with no semantic meaning. Will be included in the
105546+
payload verbatim. May be used to track mutations by the client.
105547+
"""
105548+
clientMutationId: String
105549+
_applicationId: Int!
105550+
_jsonData: JSON!
105551+
_errors: JSON!
105552+
_source: JSON!
105553+
_previousTemplate9Id: Int
105554+
}
105555+
105489105556
"""The output of our `createPackage` mutation."""
105490105557
type CreatePackagePayload {
105491105558
"""

app/tests/pages/applicantportal/form/[id]/rfi/[applicantRfiId].test.tsx

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ const mockQueryPayload = {
4848
jsonData: {},
4949
formSchemaId: 'test',
5050
},
51+
applicationFormTemplate9DataByApplicationId: {
52+
nodes: [
53+
{
54+
rowId: 1,
55+
},
56+
],
57+
},
5158
projectName: 'projName',
5259
status: 'Received',
5360
},
@@ -552,14 +559,18 @@ describe('The applicantRfiId Page', () => {
552559
rfiRowId: 1,
553560
},
554561
templateNineInput: {
555-
applicationFormTemplate9Data: {
556-
applicationId: 1,
557-
jsonData: mockSuccessResponseTemplateNine,
558-
source: {
559-
source: 'RFI',
560-
uuid: 'UUIDstring',
561-
},
562+
_applicationId: 1,
563+
_jsonData: {
564+
errors: [],
565+
projectZone: 'zone',
566+
geoName: 'geoName',
567+
},
568+
_previousTemplate9Id: 1,
569+
_source: {
570+
source: 'RFI',
571+
uuid: 'UUIDstring',
562572
},
573+
_errors: [],
563574
},
564575
}
565576
);
@@ -753,14 +764,14 @@ describe('The applicantRfiId Page', () => {
753764
rfiRowId: 1,
754765
},
755766
templateNineInput: {
756-
applicationFormTemplate9Data: {
757-
applicationId: 1,
758-
jsonData: mockSuccessResponseTemplateNine,
759-
source: {
760-
source: 'RFI',
761-
uuid: 'UUIDTemplateNine',
762-
},
767+
_previousTemplate9Id: 1,
768+
_applicationId: 1,
769+
_jsonData: mockSuccessResponseTemplateNine,
770+
_source: {
771+
source: 'RFI',
772+
uuid: 'UUIDTemplateNine',
763773
},
774+
_errors: [],
764775
},
765776
}
766777
);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
-- Deploy ccbc:mutations/create_or_update_application_form_template_9_data to pg
2+
3+
begin;
4+
5+
create or replace function ccbc_public.create_or_update_application_form_template_9_data(
6+
_application_id int,
7+
_json_data jsonb,
8+
_errors jsonb,
9+
_source jsonb,
10+
_previous_template_9_id int default null
11+
)
12+
returns ccbc_public.application_form_template_9_data as
13+
$$
14+
declare
15+
result ccbc_public.application_form_template_9_data;
16+
begin
17+
-- if previous template 9 id is provided, update the existing record
18+
-- else create a new record
19+
20+
-- return the updated or created record
21+
22+
if _previous_template_9_id is not null then
23+
update ccbc_public.application_form_template_9_data
24+
set
25+
application_id = _application_id,
26+
json_data = _json_data,
27+
errors = _errors,
28+
source = _source
29+
where id = _previous_template_9_id
30+
returning * into result;
31+
else
32+
insert into ccbc_public.application_form_template_9_data(application_id, json_data, errors, source)
33+
values (_application_id, _json_data, _errors, _source)
34+
returning * into result;
35+
end if;
36+
37+
return result;
38+
end;
39+
$$ language plpgsql volatile;
40+
41+
commit;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Revert ccbc:mutations/create_or_update_application_form_template_9_data from pg
2+
3+
BEGIN;
4+
5+
drop function ccbc_public.create_or_update_application_form_template_9_data(int, jsonb, jsonb, jsonb, int);
6+
7+
COMMIT;

db/sqitch.plan

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,3 +816,4 @@ tables/application_fnha_contribution 2025-02-24T19:40:28Z ,,, <ryohani89@NH50467
816816
mutations/save_fnha_contribution 2025-02-25T16:48:17Z ,,, <ryohani89@NH504670> # Add mutation to save fnha contribution
817817
@1.246.0 2025-03-05T22:03:48Z CCBC Service Account <[email protected]> # release v1.246.0
818818
tables/application_form_template_9_data_002_add_auth_user_role_permission 2025-02-05T15:11:34Z Anthony Bushara <[email protected]> # Adds permissions for an applicant to insert and read template 9 data
819+
mutations/create_or_update_application_form_template_9_data 2025-02-06T16:46:05Z Anthony Bushara <[email protected]> # Move the decision making of updating or creating template 9 data from frontend to the backend to create cleaner code

0 commit comments

Comments
 (0)