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 check for valid folderId #269

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions packages/api/src/brevo-api/brevo-api-folders.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as Brevo from "@getbrevo/brevo";
import { Inject, Injectable } from "@nestjs/common";
import { EmailCampaignScopeInterface } from "src/types";

import { BrevoModuleConfig } from "../config/brevo-module.config";
import { BREVO_MODULE_CONFIG } from "../config/brevo-module.constants";
import { BrevoApiFolder } from "./dto/brevo-api-folder";

@Injectable()
export class BrevoApiFoldersService {
private readonly contactsApi: Brevo.ContactsApi;

constructor(@Inject(BREVO_MODULE_CONFIG) private readonly config: BrevoModuleConfig) {
this.contactsApi = new Brevo.ContactsApi();
}

public async getFolders(scope: EmailCampaignScopeInterface): Promise<Array<BrevoApiFolder> | undefined> {
const apiKey = this.config.brevo.resolveConfig(scope).apiKey;
this.contactsApi.setApiKey(Brevo.ContactsApiApiKeys.apiKey, apiKey);

// Limit set by Brevo
const limit = 50;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you only have 50 folders in total or is the limit restricted to 50?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The limit is restricted to 50 according to the documentation: https://developers.brevo.com/reference/getfolders-1


const offset = 0;

const { response, body } = await this.contactsApi.getFolders(limit, offset);

if (response.statusCode !== 200) {
throw new Error("Failed to get folders");
}

return body.folders as BrevoApiFolder[];
}
}
5 changes: 3 additions & 2 deletions packages/api/src/brevo-api/brevo-api.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { Module } from "@nestjs/common";
import { ConfigModule } from "../config/config.module";
import { BrevoApiCampaignsService } from "./brevo-api-campaigns.service";
import { BrevoApiContactsService } from "./brevo-api-contact.service";
import { BrevoApiFoldersService } from "./brevo-api-folders.service";
import { BrevoApiSenderService } from "./brevo-api-sender.service";
import { BrevoTransactionalMailsService } from "./brevo-api-transactional-mails.service";

@Module({
imports: [ConfigModule, CacheModule.register({ ttl: 1000 * 60 }), MikroOrmModule.forFeature(["BrevoConfig"])],
providers: [BrevoApiContactsService, BrevoApiCampaignsService, BrevoTransactionalMailsService, BrevoApiSenderService],
exports: [BrevoApiContactsService, BrevoApiCampaignsService, BrevoTransactionalMailsService, BrevoApiSenderService],
providers: [BrevoApiContactsService, BrevoApiCampaignsService, BrevoTransactionalMailsService, BrevoApiSenderService, BrevoApiFoldersService],
exports: [BrevoApiContactsService, BrevoApiCampaignsService, BrevoTransactionalMailsService, BrevoApiSenderService, BrevoApiFoldersService],
})
export class BrevoApiModule {}
10 changes: 10 additions & 0 deletions packages/api/src/brevo-api/dto/brevo-api-folder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Field, ID, ObjectType } from "@nestjs/graphql";

@ObjectType()
export class BrevoApiFolder {
@Field(() => ID)
id: number;

@Field(() => String)
name: string;
}
22 changes: 22 additions & 0 deletions packages/api/src/brevo-config/brevo-config.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { InjectRepository } from "@mikro-orm/nestjs";
import { Type } from "@nestjs/common";
import { Args, ID, Mutation, Query, Resolver } from "@nestjs/graphql";

import { BrevoApiFoldersService } from "../brevo-api/brevo-api-folders.service";
import { BrevoApiSenderService } from "../brevo-api/brevo-api-sender.service";
import { BrevoTransactionalMailsService } from "../brevo-api/brevo-api-transactional-mails.service";
import { BrevoApiEmailTemplate } from "../brevo-api/dto/brevo-api-email-templates-list";
Expand All @@ -26,6 +27,7 @@ export function createBrevoConfigResolver({
constructor(
private readonly entityManager: EntityManager,
private readonly brevoSenderApiService: BrevoApiSenderService,
private readonly brevoFolderIdService: BrevoApiFoldersService,
private readonly brevoTransactionalEmailsApiService: BrevoTransactionalMailsService,
@InjectRepository(BrevoConfig) private readonly repository: EntityRepository<BrevoConfigInterface>,
) {}
Expand All @@ -50,6 +52,16 @@ export function createBrevoConfigResolver({
return false;
}

private async isValidFolderId({ folderId }: { folderId: number }): Promise<boolean> {
const folders = await this.brevoFolderIdService.getFolders(Scope);

if (folders && folders.some((folder) => folder.id === folderId)) {
return true;
}

return false;
}

@RequiredPermission(["brevo-newsletter-config"], { skipScopeCheck: true })
@Query(() => [BrevoApiSender], { nullable: true })
async senders(
Expand Down Expand Up @@ -91,6 +103,10 @@ export function createBrevoConfigResolver({
throw new Error("Template ID is not valid. ");
}

if (!(await this.isValidFolderId({ folderId: input.folderId }))) {
throw new Error("Folder ID is not valid. ");
}

const brevoConfig = this.repository.create({
...input,
scope,
Expand Down Expand Up @@ -121,6 +137,12 @@ export function createBrevoConfigResolver({
}
}

if (input.folderId) {
if (!(await this.isValidFolderId({ folderId: input.folderId }))) {
throw new Error("Folder ID is not valid. ");
}
}

if (lastUpdatedAt) {
validateNotModified(brevoConfig, lastUpdatedAt);
}
Expand Down
Loading