The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { Injectable, Logger } from '@nestjs/common';
import { isNonEmptyString } from '@sniptt/guards';
import { isDefined } from 'twenty-shared/utils';
import { UnsubscribeTokenService } from 'src/engine/core-modules/emailing-domain/services/unsubscribe-token.service';
import { MessageSuppressionService } from 'src/modules/emailing/services/message-suppression.service';
@Injectable()
export class InboundUnsubscribeHandlerService {
private readonly logger = new Logger(InboundUnsubscribeHandlerService.name);
constructor(
private readonly unsubscribeTokenService: UnsubscribeTokenService,
private readonly messageSuppressionService: MessageSuppressionService,
) {}
async handle(subject: string | null): Promise<void> {
if (!isNonEmptyString(subject)) {
this.logger.warn('Unsubscribe email received without a token subject');
return;
}
const verification = this.unsubscribeTokenService.verify(subject.trim());
if (!isDefined(verification)) {
this.logger.warn('Unsubscribe email received with an invalid token');
return;
}
const { payload } = verification;
if (payload.preview === true) {
return;
}
await this.messageSuppressionService.unsubscribeFromEverything({
workspaceId: payload.workspaceId,
emailAddress: payload.emailAddress,
});
}
}