Skip to content

Commit

Permalink
COM-1164: Add support for importing multiselect enum values (#106)
Browse files Browse the repository at this point in the history
## Description

Previously only string values could be imported in the contact import.
In case the project uses multiselect for certain contact attributes the
import was not working correctly and was throwing a validation error
since the attribute was a string but was expecting an array. Now the
contact attribute gets checked if it is supposed to be an array and gets
transformed to an array. The values need to be separated with a comma in
the import file.

## Example

[x] I have verified if my change requires an example


## Changeset

[x] I have verified if my change requires a changeset

## Related tasks and documents

COM-1164

---------

Co-authored-by: Denise Buder <[email protected]>
  • Loading branch information
RainbowBunchie and RainbowBunchie authored Oct 10, 2024
1 parent e6dc804 commit acffd63
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/wicked-doors-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@comet/brevo-api": patch
---

Support multiselect values in contact import

Previously the contact import did not support multiselect values since brevo expects an array of values and the csv import only sent values as strings. Now the import value gets transformed to an array in case the contact attribute should be of type array. The value in the csv file's column needs to be separated with a comma in case of multiple selected values.
17 changes: 14 additions & 3 deletions packages/api/src/brevo-contact/brevo-contact-import.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class BasicValidateableRow {
@IsNotEmpty()
email: string;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: string;
[key: string]: string | string[];
}

export interface CsvImportInformation {
Expand Down Expand Up @@ -165,6 +164,18 @@ export class BrevoContactImportService {
return "error";
}

private parseValue({ value, key }: { value: string; key: string }): string | string[] {
if (this.config.brevo.BrevoContactAttributes) {
const designType = Reflect.getMetadata("design:type", this.config.brevo.BrevoContactAttributes.prototype, key.toUpperCase())?.name;

if (designType === "Array") {
return value.trim() === "" ? [] : value.split(",").map((item) => item.trim());
}
}

return value;
}

private async processCsvRow(row: Record<string, string>, redirectUrlForImport: string): Promise<CreateDoubleOptInContactData> {
const mappedRow = this.createValidateableCsvRowClass();

Expand All @@ -174,7 +185,7 @@ export class BrevoContactImportService {
if (key.toLowerCase() === "email") {
mappedRow.email = row[key];
} else {
mappedRow[key.toUpperCase()] = row[key];
mappedRow[key.toUpperCase()] = this.parseValue({ value: row[key], key });
}
}

Expand Down

0 comments on commit acffd63

Please sign in to comment.