Skip to content

Commit

Permalink
changed base controller to common service
Browse files Browse the repository at this point in the history
  • Loading branch information
f-w committed Nov 16, 2024
1 parent 39594d3 commit 1b3fa1a
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 119 deletions.
105 changes: 75 additions & 30 deletions src/api/notifications/notifications.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ import { AnyObject, FilterQuery } from 'mongoose';
import { Role } from 'src/auth/constants';
import { UserProfile } from 'src/auth/dto/user-profile.dto';
import { Roles } from 'src/auth/roles.decorator';
import { CommonService } from 'src/common.service';
import { AppConfigService } from 'src/config/app-config.service';
import { promisify } from 'util';
import { BouncesService } from '../bounces/bounces.service';
import { BaseController } from '../common/base.controller';
import { CountDto } from '../common/dto/count.dto';
import { FilterDto } from '../common/dto/filter.dto';
import {
Expand Down Expand Up @@ -82,18 +82,40 @@ enum NotificationDispatchStatusField {
})
@ApiTags('notification')
@Roles(Role.Admin, Role.SuperAdmin, Role.AuthenticatedUser)
export class NotificationsController extends BaseController {
export class NotificationsController {
readonly appConfig;
readonly handleBounce;
readonly guaranteedBroadcastPushDispatchProcessing;
readonly broadcastSubscriberChunkSize;
readonly logSkippedBroadcastPushDispatches;
readonly inboundSmtpServerDomain;
readonly handleListUnsubscribeByEmail;

constructor(
private readonly notificationsService: NotificationsService,
private readonly subscriptionsService: SubscriptionsService,
readonly appConfigService: AppConfigService,
readonly configurationsService: ConfigurationsService,
private readonly bouncesService: BouncesService,
private readonly commonService: CommonService,
@Inject(REQUEST) private readonly req: Request & { user: UserProfile },
@Inject(getFlowProducerToken()) private readonly flowProducer: FlowProducer,
) {
super(appConfigService, configurationsService);
this.appConfig = appConfigService.get();
this.handleBounce = this.appConfig.email?.bounce?.enabled;
this.guaranteedBroadcastPushDispatchProcessing =
this.appConfig.notification?.guaranteedBroadcastPushDispatchProcessing;
const ft = this.appConfig.notification?.broadcastCustomFilterFunctions;

this.broadcastSubscriberChunkSize =
this.appConfig.notification?.broadcastSubscriberChunkSize;
this.logSkippedBroadcastPushDispatches =
this.appConfig.notification?.logSkippedBroadcastPushDispatches;
this.inboundSmtpServerDomain =
this.appConfig.email.inboundSmtpServer?.domain;
this.handleListUnsubscribeByEmail =
this.appConfig.email?.listUnsubscribeByEmail?.enabled;

if (ft) {
this.jmespathSearchOpts.functionTable = ft;
}
Expand Down Expand Up @@ -320,7 +342,6 @@ export class NotificationsController extends BaseController {
return this.sendPushNotification(notification);
}

handleBounce = this.appConfig.email?.bounce?.enabled;
async updateBounces(
userChannelIds: string[] | string,
dataNotification: Notification,
Expand Down Expand Up @@ -383,8 +404,6 @@ export class NotificationsController extends BaseController {
}
}

guaranteedBroadcastPushDispatchProcessing =
this.appConfig.notification?.guaranteedBroadcastPushDispatchProcessing;
async notificationMsgCB(data, err: any, e: Subscription) {
if (err) {
return this.updateBroadcastPushNotificationStatus(
Expand Down Expand Up @@ -456,14 +475,6 @@ export class NotificationsController extends BaseController {
}
}

broadcastSubscriberChunkSize =
this.appConfig.notification?.broadcastSubscriberChunkSize;
logSkippedBroadcastPushDispatches =
this.appConfig.notification?.logSkippedBroadcastPushDispatches;
inboundSmtpServerDomain = this.appConfig.email.inboundSmtpServer?.domain;
handleListUnsubscribeByEmail =
this.appConfig.email?.listUnsubscribeByEmail?.enabled;

async broadcastToSubscriberChunk(data, startIdx) {
const subChunk = (data.dispatch.candidates as string[]).slice(
startIdx,
Expand Down Expand Up @@ -535,7 +546,12 @@ export class NotificationsController extends BaseController {
}
const textBody =
data.message.textBody &&
this.mailMerge(data.message.textBody, e, data, this.req);
this.commonService.mailMerge(
data.message.textBody,
e,
data,
this.req,
);
switch (e.channel) {
case 'sms':
try {
Expand All @@ -544,7 +560,7 @@ export class NotificationsController extends BaseController {
undefined,
HttpStatus.INTERNAL_SERVER_ERROR,
);
await this.sendSMS(e.userChannelId, textBody, e);
await this.commonService.sendSMS(e.userChannelId, textBody, e);
return await this.notificationMsgCB(data, null, e);
} catch (ex) {
return await this.notificationMsgCB(data, ex, e);
Expand All @@ -553,11 +569,21 @@ export class NotificationsController extends BaseController {
default: {
const subject =
data.message.subject &&
this.mailMerge(data.message.subject, e, data, this.req);
this.commonService.mailMerge(
data.message.subject,
e,
data,
this.req,
);
const htmlBody =
data.message.htmlBody &&
this.mailMerge(data.message.htmlBody, e, data, this.req);
const unsubscriptUrl = this.mailMerge(
this.commonService.mailMerge(
data.message.htmlBody,
e,
data,
this.req,
);
const unsubscriptUrl = this.commonService.mailMerge(
'{unsubscription_url}',
e,
data,
Expand All @@ -569,7 +595,7 @@ export class NotificationsController extends BaseController {
this.inboundSmtpServerDomain
) {
const unsubEmail =
this.mailMerge(
this.commonService.mailMerge(
'un-{subscription_id}-{unsubscription_code}@',
e,
data,
Expand All @@ -589,7 +615,7 @@ export class NotificationsController extends BaseController {
},
};
if (this.handleBounce && this.inboundSmtpServerDomain) {
const bounceEmail = this.mailMerge(
const bounceEmail = this.commonService.mailMerge(
`bn-{subscription_id}-{unsubscription_code}@${this.inboundSmtpServerDomain}`,
e,
data,
Expand All @@ -606,7 +632,7 @@ export class NotificationsController extends BaseController {
undefined,
HttpStatus.INTERNAL_SERVER_ERROR,
);
await this.sendEmail(mailOptions);
await this.commonService.sendEmail(mailOptions);
return await this.notificationMsgCB(data, null, e);
} catch (ex) {
return await this.notificationMsgCB(data, ex, e);
Expand All @@ -624,19 +650,38 @@ export class NotificationsController extends BaseController {
this.req['NotifyBC.subscription'] ?? {};
const textBody =
data.message.textBody &&
this.mailMerge(data.message.textBody, sub, data, this.req);
this.commonService.mailMerge(
data.message.textBody,
sub,
data,
this.req,
);
switch (data.channel) {
case 'sms':
await this.sendSMS(data.userChannelId as string, textBody, sub);
await this.commonService.sendSMS(
data.userChannelId as string,
textBody,
sub,
);
return;
default: {
const htmlBody =
data.message.htmlBody &&
this.mailMerge(data.message.htmlBody, sub, data, this.req);
this.commonService.mailMerge(
data.message.htmlBody,
sub,
data,
this.req,
);
const subject =
data.message.subject &&
this.mailMerge(data.message.subject, sub, data, this.req);
const unsubscriptUrl = this.mailMerge(
this.commonService.mailMerge(
data.message.subject,
sub,
data,
this.req,
);
const unsubscriptUrl = this.commonService.mailMerge(
'{unsubscription_url}',
sub,
data,
Expand All @@ -648,7 +693,7 @@ export class NotificationsController extends BaseController {
this.inboundSmtpServerDomain
) {
const unsubEmail =
this.mailMerge(
this.commonService.mailMerge(
'un-{subscription_id}-{unsubscription_code}@',
sub,
data,
Expand All @@ -668,7 +713,7 @@ export class NotificationsController extends BaseController {
},
};
if (this.handleBounce && this.inboundSmtpServerDomain) {
const bounceEmail = this.mailMerge(
const bounceEmail = this.commonService.mailMerge(
`bn-{subscription_id}-{unsubscription_code}@${this.inboundSmtpServerDomain}`,
sub,
data,
Expand All @@ -679,7 +724,7 @@ export class NotificationsController extends BaseController {
to: data.userChannelId,
};
}
await this.sendEmail(mailOptions);
await this.commonService.sendEmail(mailOptions);
await this.updateBounces(data.userChannelId as string, data);
return;
}
Expand Down
5 changes: 0 additions & 5 deletions src/api/notifications/notifications.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ import { NotificationsService } from './notifications.service';
MongooseModule.forFeature([
{ name: Notification.name, schema: NotificationSchema },
]),
BullModule.registerQueue(
{ name: 's' /* sms */ },
{ name: 'e' /* email */ },
{ name: 'n' /* notification */ },
),
BullModule.registerFlowProducer({}),
],
controllers: [NotificationsController],
Expand Down
Loading

0 comments on commit 1b3fa1a

Please sign in to comment.