Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add folder id to config #159

Merged
merged 18 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/new-cobras-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@comet/brevo-admin": minor
"@comet/brevo-api": minor
---

Add `folderId` to `BrevoConfig` to allow overwriting the default folderId `1`
3 changes: 3 additions & 0 deletions demo/api/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ type BrevoConfig implements DocumentInterface {
updatedAt: DateTime!
senderMail: String!
senderName: String!
folderId: Int!
createdAt: DateTime!
scope: EmailCampaignContentScope!
}
Expand Down Expand Up @@ -997,9 +998,11 @@ input TargetGroupUpdateInput {
input BrevoConfigInput {
senderMail: String!
senderName: String!
folderId: Int!
}

input BrevoConfigUpdateInput {
senderMail: String
senderName: String
folderId: Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const brevoConfigFormFragment = gql`
fragment BrevoConfigForm on BrevoConfig {
senderMail
senderName
folderId
}
`;

Expand Down
30 changes: 29 additions & 1 deletion packages/admin/src/brevoConfiguration/BrevoConfigForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import {
FinalFormSubmitEvent,
Loading,
MainContent,
NumberField,
Toolbar,
ToolbarActions,
ToolbarFillSpace,
ToolbarTitleItem,
Tooltip,
useFormApiRef,
useStackSwitchApi,
} from "@comet/admin";
import { Info } from "@comet/admin-icons";
import { ContentScopeIndicator, ContentScopeInterface, resolveHasSaveConflict, useFormSaveConflict } from "@comet/cms-admin";
import { FormApi } from "final-form";
import React from "react";
Expand All @@ -37,6 +40,7 @@ interface Option {
}
type FormValues = {
sender: Option;
folderId: number;
};

interface FormProps {
Expand Down Expand Up @@ -76,9 +80,10 @@ export function BrevoConfigForm({ scope }: FormProps): React.ReactElement {
value: sender.id,
label: `${sender.name} (${sender.email})`,
},
folderId: data?.brevoConfig?.folderId ?? 1,
}
: {};
}, [data?.brevoConfig?.senderMail, data?.brevoConfig?.senderName, sendersData?.senders]);
}, [data?.brevoConfig?.folderId, data?.brevoConfig?.senderMail, data?.brevoConfig?.senderName, sendersData?.senders]);

const saveConflict = useFormSaveConflict({
checkConflict: async () => {
Expand Down Expand Up @@ -117,6 +122,7 @@ export function BrevoConfigForm({ scope }: FormProps): React.ReactElement {
const output = {
senderName: sender?.name,
senderMail: sender?.email,
folderId: state.folderId ?? 1,
};

if (mode === "edit") {
Expand Down Expand Up @@ -174,6 +180,28 @@ export function BrevoConfigForm({ scope }: FormProps): React.ReactElement {
label={<FormattedMessage id="cometBrevoModule.brevoConfig.sender" defaultMessage="Sender" />}
fullWidth
/>
<NumberField
name="folderId"
defaultValue={1}
label={
<>
<FormattedMessage id="cometBrevoModule.brevoConfig.folderId" defaultMessage="Folder ID" />
<Tooltip
title={
<FormattedMessage
id="cometBrevoModule.brevoConfig.folderId.info"
defaultMessage="By default, the folder ID should be set to 1 unless you have specifically configured another folder in Brevo."
/>
}
sx={{ marginLeft: "5px" }}
>
<Info />
</Tooltip>
</>
}
fullWidth
required
/>
</MainContent>
</>
);
Expand Down
15 changes: 9 additions & 6 deletions packages/api/schema.gql
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
type User {
id: String!
name: String!
email: String!
}

type CurrentUserPermission {
permission: String!
contentScopes: [JSONObject!]!
Expand Down Expand Up @@ -37,12 +43,6 @@ enum FocalPoint {
SOUTHEAST
}

type User {
id: String!
name: String!
email: String!
}

type BrevoApiSender {
id: ID!
name: String!
Expand Down Expand Up @@ -185,6 +185,7 @@ type BrevoConfig implements DocumentInterface {
updatedAt: DateTime!
senderMail: String!
senderName: String!
folderId: Int!
createdAt: DateTime!
scope: EmailCampaignContentScope!
}
Expand Down Expand Up @@ -359,9 +360,11 @@ input SendTestEmailCampaignArgs {
input BrevoConfigInput {
senderMail: String!
senderName: String!
folderId: Int!
}

input BrevoConfigUpdateInput {
senderMail: String
senderName: String
folderId: Int
}
12 changes: 10 additions & 2 deletions packages/api/src/brevo-api/brevo-api-contact.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as Brevo from "@getbrevo/brevo";
import { EntityRepository } from "@mikro-orm/core";
import { InjectRepository } from "@mikro-orm/nestjs";
import { Inject, Injectable } from "@nestjs/common";
import { BrevoConfigInterface } from "src/brevo-config/entities/brevo-config-entity.factory";
import { BrevoContactAttributesInterface, EmailCampaignScopeInterface } from "src/types";

import { BrevoContactInterface } from "../brevo-contact/dto/brevo-contact.factory";
Expand All @@ -18,7 +21,10 @@ export interface CreateDoubleOptInContactData {
export class BrevoApiContactsService {
private readonly contactsApis = new Map<string, Brevo.ContactsApi>();

constructor(@Inject(BREVO_MODULE_CONFIG) private readonly config: BrevoModuleConfig) {}
constructor(
@Inject(BREVO_MODULE_CONFIG) private readonly config: BrevoModuleConfig,
@InjectRepository("BrevoConfig") private readonly brevoConfigRepository: EntityRepository<BrevoConfigInterface>,
) {}

private getContactsApi(scope: EmailCampaignScopeInterface): Brevo.ContactsApi {
try {
Expand Down Expand Up @@ -186,10 +192,12 @@ export class BrevoApiContactsService {
}

public async createBrevoContactList(title: string, scope: EmailCampaignScopeInterface): Promise<number | undefined> {
const brevoConfig = await this.brevoConfigRepository.findOne({ scope });

try {
const contactList = {
name: title,
folderId: 1, // folderId is required, folder #1 is created by default
folderId: brevoConfig?.folderId ?? 1,
};

const data = await this.getContactsApi(scope).createList(contactList);
Expand Down
8 changes: 6 additions & 2 deletions packages/api/src/brevo-config/dto/brevo-config.input.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PartialType } from "@comet/cms-api";
import { Field, InputType } from "@nestjs/graphql";
import { IsEmail, IsNotEmpty, IsString } from "class-validator";
import { Field, InputType, Int } from "@nestjs/graphql";
import { IsEmail, IsInt, IsNotEmpty, IsString } from "class-validator";

@InputType()
export class BrevoConfigInput {
Expand All @@ -14,6 +14,10 @@ export class BrevoConfigInput {
@IsString()
@Field()
senderName: string;

@Field(() => Int)
@IsInt()
folderId: number;
}

@InputType()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DocumentInterface } from "@comet/cms-api";
import { Embedded, Entity, OptionalProps, PrimaryKey, Property } from "@mikro-orm/core";
import { Type } from "@nestjs/common";
import { Field, ID, ObjectType } from "@nestjs/graphql";
import { Field, ID, Int, ObjectType } from "@nestjs/graphql";
import { v4 } from "uuid";

import { EmailCampaignScopeInterface } from "../../types";
Expand All @@ -11,6 +11,7 @@ export interface BrevoConfigInterface {
id: string;
senderName: string;
senderMail: string;
folderId: number;
createdAt: Date;
updatedAt: Date;
scope: EmailCampaignScopeInterface;
Expand All @@ -37,6 +38,10 @@ export class BrevoConfigEntityFactory {
@Field()
senderName: string;

@Property({ columnType: "int" })
@Field(() => Int)
folderId: number;

@Property({
columnType: "timestamp with time zone",
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Migration } from "@mikro-orm/migrations";

export class Migration20241119101706 extends Migration {
async up(): Promise<void> {
this.addSql('alter table "BrevoConfig" add column "folderId" int not null default 1;');
this.addSql('alter table "BrevoConfig" alter column "folderId" drop default;');
}
}
2 changes: 2 additions & 0 deletions packages/api/src/mikro-orm/migrations/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Migration20240619145217 } from "./Migration20240619145217";
import { Migration20240621102349 } from "./Migration20240621102349";
import { Migration20240819214939 } from "./Migration20240819214939";
import { Migration20241016123307 } from "./Migration20241016123307";
import { Migration20241119101706 } from "./Migration20241119101706";

export const migrationsList: MigrationObject[] = [
{ name: "Migration20240115095733", class: Migration20240115095733 },
Expand All @@ -20,4 +21,5 @@ export const migrationsList: MigrationObject[] = [
{ name: "Migration20240621102349", class: Migration20240621102349 },
{ name: "Migration20240819214939", class: Migration20240819214939 },
{ name: "Migration20241016123307", class: Migration20241016123307 },
{ name: "Migration20241119101706", class: Migration20241119101706 },
];
Loading