Skip to content

Commit

Permalink
fix: error and update menstrual calendar indexer (#200)
Browse files Browse the repository at this point in the history
* fix error and update menstrual calendar indexer

* update menstrual cycle log
  • Loading branch information
rubenkristian authored Oct 27, 2022
1 parent 0a8a240 commit 926b2f7
Show file tree
Hide file tree
Showing 19 changed files with 130 additions and 79 deletions.
22 changes: 22 additions & 0 deletions src/common/mailer/mailer.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { MailerService } from '@nestjs-modules/mailer';
import { Injectable, Logger } from '@nestjs/common';
import { keyList } from '../../common/secrets';
import { CustomerStakingRequestService, LabRegister } from './models';
import { NewOrderGA } from './models/new-order-ga.model';
import { NewOrderLab } from './models/new-order-lab.model';

@Injectable()
export class MailerManager {
Expand All @@ -12,6 +14,26 @@ export class MailerManager {
private readonly mailerService: MailerService,
) {}

async sendNewOrderToLab(to: string, context: NewOrderLab) {
const subject = `New Order #1`;
await this.mailerService.sendMail({
to: to,
subject: subject,
template: 'new-order-lab',
context: context,
});
}

async sendNewOrderToGa(to: string, context: NewOrderGA) {
const subject = `New Order #1`;
await this.mailerService.sendMail({
to: to,
subject: subject,
template: 'new-order-ga',
context: context,
});
}

async sendCustomerStakingRequestServiceEmail(
to: string | string[],
context: CustomerStakingRequestService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ export class MenstrualCalendarAddedHandler
constructor(private readonly elasticsearchService: ElasticsearchService) {}

async execute(command: MenstrualCalendarAddedCommandIndexer) {
const { menstrualCalendar, blockMetaData } = command;
const {
menstrualCalendar: { id, addressId, averageCycle, cycleLog, createdAt },
blockMetaData,
} = command;
await this.elasticsearchService.create({
index: 'menstrual-calendar',
id: menstrualCalendar.id,
id: id,
refresh: 'wait_for',
body: {
address_id: menstrualCalendar.addressId,
average_cycle: menstrualCalendar.averageCycle,
cycle_log: menstrualCalendar.cycleLog,
created_at: menstrualCalendar.createdAt,
updated_at: menstrualCalendar.updatedAt,
menstrual_calendar_id: id,
address_id: addressId,
average_cycle: averageCycle,
cycle_log: cycleLog,
created_at: createdAt.getTime(),
updated_at: null,
blockMetaData: blockMetaData,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ export class MenstrualCalendarUpdatedHandler
constructor(private readonly elasticsearchService: ElasticsearchService) {}

async execute(command: MenstrualCalendarUpdatedCommandIndexer) {
const { menstrualCalendar, blockMetaData } = command;
const {
menstrualCalendar: { id, addressId, averageCycle, cycleLog, updatedAt },
blockMetaData,
} = command;
await this.elasticsearchService.update({
index: 'menstrual-calendar',
id: menstrualCalendar.id,
id: id,
refresh: 'wait_for',
body: {
doc: {
address_id: menstrualCalendar.addressId,
average_cycle: menstrualCalendar.averageCycle,
cycle_log: menstrualCalendar.cycleLog,
created_at: menstrualCalendar.createdAt,
updated_at: menstrualCalendar.updatedAt,
address_id: addressId,
average_cycle: averageCycle,
cycle_log: cycleLog,
updated_at: updatedAt.getTime(),
blockMetaData: blockMetaData,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { BlockMetaData } from '../../../../models/block-meta-data';

export class MenstrualCycleLogAddedCommandIndexer {
menstrualCycleLog: MenstrualCycleLog;
accountId: string;
constructor(data: Array<any>, public readonly blockMetaData: BlockMetaData) {
const menstrualCycleLogData = data[0];
this.accountId = data[1].toString();
this.menstrualCycleLog = new MenstrualCycleLog(
menstrualCycleLogData.toHuman(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,31 @@ export class MenstrualCycleLogAddedHandler
constructor(private readonly elasticsearchService: ElasticsearchService) {}

async execute(command: MenstrualCycleLogAddedCommandIndexer) {
const { menstrualCycleLog, blockMetaData } = command;
const {
menstrualCycleLog: {
id,
menstrualCalendarId,
date,
menstruation,
symptoms,
createdAt,
},
accountId,
blockMetaData,
} = command;
await this.elasticsearchService.create({
index: 'menstrual-cycle-log',
id: menstrualCycleLog.id,
id: id,
refresh: 'wait_for',
body: {
menstrual_calendar_id: menstrualCycleLog.menstrualCalendarId,
date: menstrualCycleLog.date,
menstruation: menstrualCycleLog.menstruation,
symptoms: menstrualCycleLog.symptoms,
created_at: menstrualCycleLog.createdAt,
updated_at: menstrualCycleLog.updatedAt,
menstrual_calendar_cycle_log_id: id,
account_id: accountId,
menstrual_calendar_id: menstrualCalendarId,
date: date,
menstruation: menstruation,
symptoms: symptoms,
created_at: createdAt.getTime(),
updated_at: null,
blockMetaData: blockMetaData,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ export class MenstrualCycleLogRemovedHandler
constructor(private readonly elasticsearchService: ElasticsearchService) {}

async execute(command: MenstrualCycleLogRemovedCommandIndexer) {
const { menstrualCycleLog } = command;
const {
menstrualCycleLog: { id },
} = command;
await this.elasticsearchService.delete({
index: 'menstrual-cycle-log',
id: menstrualCycleLog.id,
id: id,
refresh: 'wait_for',
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,28 @@ export class MenstrualCycleLogUpdatedHandler
constructor(private readonly elasticsearchService: ElasticsearchService) {}

async execute(command: MenstrualCycleLogUpdatedCommandIndexer) {
const { menstrualCycleLog, blockMetaData } = command;
const {
menstrualCycleLog: {
id,
menstrualCalendarId,
date,
menstruation,
symptoms,
updatedAt,
},
blockMetaData,
} = command;
await this.elasticsearchService.update({
index: 'menstrual-cycle-log',
id: menstrualCycleLog.id,
id: id,
refresh: 'wait_for',
body: {
doc: {
menstrual_calendar_id: menstrualCycleLog.menstrualCalendarId,
date: menstrualCycleLog.date,
menstruation: menstrualCycleLog.menstruation,
symptoms: menstrualCycleLog.symptoms,
updated_at: menstrualCycleLog.updatedAt,
menstrual_calendar_id: menstrualCalendarId,
date: date,
menstruation: menstruation,
symptoms: symptoms,
updated_at: updatedAt.getTime(),
blockMetaData: blockMetaData,
},
},
Expand Down
2 changes: 2 additions & 0 deletions src/indexer/indexer.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ export class IndexerHandler
'services',
'orders',
'last-block-number-substrate',
'menstrual-calendar',
'menstrual-cycle-log',
];

for (const i of indices) {
Expand Down
10 changes: 7 additions & 3 deletions src/indexer/models/menstrual-calendar/menstrual-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ export class MenstrualCalendar {
this.addressId = menstrualCalendar.addressId;
this.averageCycle = menstrualCalendar.averageCycle;
this.cycleLog = menstrualCalendar.cycleLog;
this.createdAt = menstrualCalendar.createdAt;
this.updatedAt = menstrualCalendar.updatedAt;
this.createdAt = new Date(
String(menstrualCalendar.createdAt).split(',').join(''),
);
this.updatedAt = menstrualCalendar?.updatedAt
? new Date(String(menstrualCalendar.updatedAt).split(',').join(''))
: null;
}

id: string;
addressId: string;
averageCycle: number;
cycleLog: Array<string>;
createdAt: Date;
updatedAt: Date;
updatedAt: Date | null;
}
11 changes: 8 additions & 3 deletions src/indexer/models/menstrual-calendar/menstrual-cycle-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ export class MenstrualCycleLog {
constructor(menstrualCycleLog: any) {
this.id = menstrualCycleLog.id;
this.menstrualCalendarId = menstrualCycleLog.menstrualCalendarId;
this.date = menstrualCycleLog.date;
this.date = new Date(String(menstrualCycleLog.date).split(',').join(''));
this.menstruation = menstrualCycleLog.menstruation;
this.symptoms = menstrualCycleLog.symptoms;
this.createdAt = menstrualCycleLog.createdAt;
this.createdAt = new Date(
String(menstrualCycleLog.createdAt).split(',').join(''),
);
this.updatedAt = menstrualCycleLog?.updatedAt
? new Date(String(menstrualCycleLog.updatedAt).split(',').join())
: null;
}

id: string;
Expand All @@ -14,7 +19,7 @@ export class MenstrualCycleLog {
menstruation: boolean;
symptoms: Array<Symptom>;
createdAt: Date;
updatedAt: Date;
updatedAt: Date | null;
}

export class Symptom {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { TransactionLoggingDto } from '../../../../../common/transaction-logging/dto/transaction-logging.dto';
import {
DateTimeProxy,
MailerManager,
NotificationService,
SubstrateService,
TransactionLoggingService,
Expand All @@ -13,8 +14,7 @@ import {
queryGeneticAnalystByAccountId,
queryGeneticAnalystServicesByHashId,
} from '@debionetwork/polkadot-provider';
import { NewOrderGA } from '../../../models/new-order-ga.model';
import { MailerService } from '@nestjs-modules/mailer';
import { NewOrderGA } from '../../../../../common/mailer/models/new-order-ga.model';
import { GCloudSecretManagerService } from '@debionetwork/nestjs-gcloud-secret-manager';
import { keyList } from '../../../../../common/secrets';

Expand All @@ -32,7 +32,7 @@ export class GeneticAnalysisOrderPaidHandler
private readonly notificationService: NotificationService,
private readonly dateTimeProxy: DateTimeProxy,
private readonly substrateService: SubstrateService,
private readonly mailerService: MailerService,
private readonly mailerManager: MailerManager,
private readonly gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
) {}

Expand Down Expand Up @@ -101,26 +101,19 @@ export class GeneticAnalysisOrderPaidHandler
.getSecret('GA_ORDER_LINK')
?.toString() ?? '' + geneticAnalysisOrder.id;

await this.sendNewOrderToGa(geneticAnaystDetail.info.email, {
service: geneticAnalystServiceDetail.info.name,
price:
geneticAnalystServiceDetail.info.pricesByCurrency[0].totalPrice.toString(),
order_id: geneticAnalysisOrder.id,
order_date: geneticAnalysisOrder.createdAt.toDateString(),
link_order: linkOrder,
});
await this.mailerManager.sendNewOrderToGa(
geneticAnaystDetail.info.email,
{
service: geneticAnalystServiceDetail.info.name,
price:
geneticAnalystServiceDetail.info.pricesByCurrency[0].totalPrice.toString(),
order_id: geneticAnalysisOrder.id,
order_date: geneticAnalysisOrder.createdAt.toDateString(),
link_order: linkOrder,
},
);
} catch (error) {
this.logger.log(error);
}
}

async sendNewOrderToGa(to: string, context: NewOrderGA) {
const subject = `New Order #1`;
await this.mailerService.sendMail({
to: to,
subject: subject,
template: 'new-order-ga',
context: context,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { OrderPaidCommand } from './order-paid.command';
import {
DateTimeProxy,
MailerManager,
NotificationService,
SubstrateService,
TransactionLoggingService,
Expand All @@ -16,7 +17,6 @@ import {
import { NotificationDto } from '../../../../../common/notification/dto/notification.dto';
import { MailerService } from '@nestjs-modules/mailer';
import { GCloudSecretManagerService } from '@debionetwork/nestjs-gcloud-secret-manager';
import { NewOrderLab } from '../../../models/new-order-lab.model';
import { keyList } from '../../../../../common/secrets';

@Injectable()
Expand All @@ -29,7 +29,7 @@ export class OrderPaidHandler implements ICommandHandler<OrderPaidCommand> {
private readonly notificationService: NotificationService,
private readonly dateTimeProxy: DateTimeProxy,
private readonly substrateService: SubstrateService,
private readonly mailerService: MailerService,
private readonly mailerManager: MailerManager,
private readonly gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
) {}

Expand Down Expand Up @@ -94,7 +94,7 @@ export class OrderPaidHandler implements ICommandHandler<OrderPaidCommand> {
.getSecret('LAB_ORDER_LINK')
.toString() ?? '' + order.id;

await this.sendNewOrderToLab(labDetail.info.email, {
await this.mailerManager.sendNewOrderToLab(labDetail.info.email, {
specimen_number: order.dnaSampleTrackingId,
service: serviceDetail.info.name,
service_price: serviceDetail.price,
Expand All @@ -108,14 +108,4 @@ export class OrderPaidHandler implements ICommandHandler<OrderPaidCommand> {
this.logger.log(error);
}
}

async sendNewOrderToLab(to: string, context: NewOrderLab) {
const subject = `New Order #1`;
await this.mailerService.sendMail({
to: to,
subject: subject,
template: 'new-order-lab',
context: context,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ServiceRequestUnstakedCommand,
ServiceRequestWaitingForUnstakedCommand,
ServiceRequestStakingAmountExcessRefundedCommand,
ServiceRequestUpdatedCommand,
} from './commands/service-request';
import {
OrderCancelledCommand,
Expand Down Expand Up @@ -75,6 +76,7 @@ const eventRoutes = {
StakingAmountExcessRefunded:
ServiceRequestStakingAmountExcessRefundedCommand,
StakingAmountRefunded: ServiceRequestStakingAmountRefundedCommand,
ServiceRequestUpdated: ServiceRequestUpdatedCommand,
},
orders: {
OrderCreated: OrderCreatedCommand,
Expand Down
Loading

0 comments on commit 926b2f7

Please sign in to comment.