Skip to content

Commit 4c9066b

Browse files
committed
Merge branch 'contact-notes' into stage-main-12-3
2 parents 9f62dde + 56b14bf commit 4c9066b

4 files changed

Lines changed: 47 additions & 17 deletions

File tree

src/extensions/texter-sideboxes/contact-notes/react-component.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,34 @@ export const showSidebox = ({ contact }) => {
1515
return contact;
1616
};
1717

18-
export class TexterSideboxClass extends React.Component {
19-
constructor(props) {
20-
let customFields = props.contact.customFields;
21-
if (typeof props.contact.customFields === "string") {
22-
try {
23-
customFields = JSON.parse(props.contact.customFields || "{}");
24-
} catch (err) {
25-
// eslint-disable-next-line no-console
26-
console.warn(err);
27-
customFields = {};
28-
}
18+
const parseCustomFields = customFields => {
19+
if (typeof customFields === "string") {
20+
try {
21+
customFields = JSON.parse(customFields || "{}");
22+
} catch (err) {
23+
// eslint-disable-next-line no-console
24+
console.warn(err);
25+
customFields = {};
2926
}
27+
}
28+
return customFields;
29+
};
3030

31+
export class TexterSideboxClass extends React.Component {
32+
constructor(props) {
33+
let parsedCustomFields = parseCustomFields(props.contact.customFields);
3134
super(props);
3235
this.state = {
33-
notes: customFields.notes || "",
36+
parsedCustomFields,
37+
notes: parsedCustomFields.notes || "",
3438
isSaving: false,
3539
hasError: false
3640
};
3741
}
3842

3943
componentDidUpdate() {
40-
const { notes: contactNotes } = this.props.contact.customFields;
44+
const { contact } = this.props;
45+
const { notes: contactNotes } = parseCustomFields(contact.customFields);
4146
if (this.state.isSaving && contactNotes === this.state.notes) {
4247
this.setIsSaving(false);
4348
}
@@ -47,10 +52,11 @@ export class TexterSideboxClass extends React.Component {
4752

4853
debouncedUpdate = _.debounce(
4954
async notes => {
55+
const { parsedCustomFields } = this.state;
5056
const { contact, mutations } = this.props;
5157
try {
5258
const customFields = {
53-
...contact.customFields,
59+
...parsedCustomFields,
5460
notes
5561
};
5662
await mutations.updateContactCustomFields(JSON.stringify(customFields));

src/lib/scripts.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ const getScriptFieldValue = (contact, texter, fieldName) => {
8989
} else if (TOP_LEVEL_UPLOAD_FIELDS.indexOf(fieldName) !== -1) {
9090
result = contact[fieldName];
9191
} else {
92-
const customFieldNames = JSON.parse(contact.customFields);
92+
let customFieldNames = contact.customFields;
93+
if (typeof customFieldNames === "string") {
94+
customFieldNames = JSON.parse(contact.customFields);
95+
}
9396
result = customFieldNames[fieldName];
9497
}
9598

src/server/api/schema.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,11 @@ const rootMutations = {
12511251
(!cannedResponses.length || cannedResponses[0].usedFields)
12521252
) {
12531253
const usedFields = campaign.usedFields;
1254+
1255+
const texterSideboxes = getConfig("TEXTER_SIDEBOXES") || "";
1256+
const shouldUseNotes = /contact-notes/.test(texterSideboxes);
1257+
if (shouldUseNotes) usedFields.notes = 1;
1258+
12541259
if (cannedResponses.length && cannedResponses[0].usedFields) {
12551260
Object.keys(cannedResponses[0].usedFields).forEach(f => {
12561261
usedFields[f] = 1;

src/server/models/cacheable_queries/campaign-contact.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,25 @@ const campaignContactCache = {
563563
updateCustomFields: async (contact, customFields, campaign, organization) => {
564564
/* eslint-disable no-param-reassign */
565565
try {
566-
if (typeof customFields !== "string") {
567-
customFields = JSON.stringify(customFields || {});
566+
if (typeof customFields === "string") {
567+
customFields = JSON.parse(customFields || "{}");
568568
}
569+
/* we may not have all the customFields from the client,
570+
so we need to fetch from the db and merge all of them */
571+
const existingContact = await r
572+
.knex("campaign_contact")
573+
.select("custom_fields")
574+
.where("id", contact.id)
575+
.first();
576+
const existingCustomFields = JSON.parse(
577+
existingContact.custom_fields || "{}"
578+
);
579+
580+
customFields = JSON.stringify({
581+
...existingCustomFields,
582+
...customFields
583+
});
584+
569585
const updatedAt = new Date();
570586
await r
571587
.knex("campaign_contact")

0 commit comments

Comments
 (0)