Skip to content

Commit

Permalink
feat: second opinion indexer handler (#256)
Browse files Browse the repository at this point in the history
second opinion indexer handler
  • Loading branch information
rubenkristian authored Jan 5, 2023
1 parent 8e87af6 commit 30a6067
Show file tree
Hide file tree
Showing 21 changed files with 351 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BlockMetaData } from '../../../../models/block-meta-data';
import { OpinionRequestor } from '@indexer/models/opinion-requestor';

export class OpinionRequestedCommandIndexer {
accountId: string;
opinionRequestor: OpinionRequestor;
constructor(data: Array<any>, public readonly blockMetaData: BlockMetaData) {
this.accountId = data[0].toString();
this.opinionRequestor = new OpinionRequestor(data[1].toHuman());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { ElasticsearchService } from '@nestjs/elasticsearch';
import { OpinionRequestedCommandIndexer } from './opinion-requested.command';

@Injectable()
@CommandHandler(OpinionRequestedCommandIndexer)
export class OpinionRequestedHandler
implements ICommandHandler<OpinionRequestedCommandIndexer>
{
constructor(private readonly elasticsearchService: ElasticsearchService) {}

async execute(command: OpinionRequestedCommandIndexer) {
const { opinionRequestor, blockMetaData } = command;

await this.elasticsearchService.create({
index: 'opinion-requestor',
id: opinionRequestor.id,
refresh: 'wait_for',
body: {
...opinionRequestor,
blockMetaData: blockMetaData,
},
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BlockMetaData } from '../../../../models/block-meta-data';
import { OpinionRequestor } from '@indexer/models/opinion-requestor';

export class OpinionRequestorInfoUpdatedCommandIndexer {
accountId: string;
opinionRequestor: OpinionRequestor;
constructor(data: Array<any>, public readonly blockMetaData: BlockMetaData) {
this.accountId = data[0].toString();
this.opinionRequestor = new OpinionRequestor(data[1].toHuman());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Injectable } from '@nestjs/common';
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { ElasticsearchService } from '@nestjs/elasticsearch';
import { OpinionRequestorInfoUpdatedCommandIndexer } from './opinion-requestor-info-updated.command';

@Injectable()
@CommandHandler(OpinionRequestorInfoUpdatedCommandIndexer)
export class OpinionRequestorInfoUpdatedHandler
implements ICommandHandler<OpinionRequestorInfoUpdatedCommandIndexer>
{
constructor(private readonly elasticsearchService: ElasticsearchService) {}

async execute(command: OpinionRequestorInfoUpdatedCommandIndexer) {
const { opinionRequestor, blockMetaData } = command;

await this.elasticsearchService.update({
index: 'opinion-requestor',
id: opinionRequestor.id,
refresh: 'wait_for',
body: {
doc: {
blockMetaData: blockMetaData,
},
},
});
}
}
10 changes: 10 additions & 0 deletions src/indexer/events/opinion-requestor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export * from './commands/opinion-requested/opinion-requested.command';
export * from './commands/opinion-requestor-info-updated/opinion-requestor-info-updated.command';

import { OpinionRequestedHandler } from './commands/opinion-requested/opinion-requested.handler';
import { OpinionRequestorInfoUpdatedHandler } from './commands/opinion-requestor-info-updated/opinion-requestor-info-updated.handler';

export const OpinionRequestorCommandHandlers = [
OpinionRequestedHandler,
OpinionRequestorInfoUpdatedHandler,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BlockMetaData } from '../../../../models/block-meta-data';
import { Opinion } from '@indexer/models/opinion';

export class OpinionAddedCommandIndexer {
accountId: string;
opinion: Opinion;
constructor(data: Array<any>, public readonly blockMetaData: BlockMetaData) {
this.accountId = data[0].toString();
this.opinion = new Opinion(data[1].toHuman());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { ElasticsearchService } from '@nestjs/elasticsearch';
import { OpinionAddedCommandIndexer } from './opinion-added.command';

@Injectable()
@CommandHandler(OpinionAddedCommandIndexer)
export class OpinionAddedHandler
implements ICommandHandler<OpinionAddedCommandIndexer>
{
constructor(private readonly elasticsearchService: ElasticsearchService) {}

async execute(command: OpinionAddedCommandIndexer) {
const { opinion, blockMetaData } = command;

await this.elasticsearchService.create({
index: 'opinion',
id: opinion.id,
refresh: 'wait_for',
body: {
...opinion,
blockMetaData: blockMetaData,
},
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BlockMetaData } from '../../../../models/block-meta-data';

export class OpinionRemovedCommandIndexer {
accountId: string;
hash: string;
constructor(data: Array<any>, public readonly blockMetaData: BlockMetaData) {
this.accountId = data[0].toString();
this.hash = data[1].toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@nestjs/common';
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { ElasticsearchService } from '@nestjs/elasticsearch';
import { OpinionRemovedCommandIndexer } from './opinion-removed.command';

@Injectable()
@CommandHandler(OpinionRemovedCommandIndexer)
export class OpinionRemovedHandler
implements ICommandHandler<OpinionRemovedCommandIndexer>
{
constructor(private readonly elasticsearchService: ElasticsearchService) {}

async execute(command: OpinionRemovedCommandIndexer) {
const { hash } = command;

await this.elasticsearchService.delete({
index: 'opinion',
id: hash,
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { BlockMetaData } from '../../../../models/block-meta-data';

export class OpinionStatusUpdatedCommandIndexer {
accountId: string;
hash: string;
status: string;
constructor(data: Array<any>, public readonly blockMetaData: BlockMetaData) {
this.accountId = data[0].toString();
this.hash = data[1].toString();
this.status = data[2].toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Injectable } from '@nestjs/common';
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { ElasticsearchService } from '@nestjs/elasticsearch';
import { OpinionStatusUpdatedCommandIndexer } from './opinion-status-updated.command';

@Injectable()
@CommandHandler(OpinionStatusUpdatedCommandIndexer)
export class OpinionStatusUpdatedHandler
implements ICommandHandler<OpinionStatusUpdatedCommandIndexer>
{
constructor(private readonly elasticsearchService: ElasticsearchService) {}

async execute(command: OpinionStatusUpdatedCommandIndexer) {
const { hash, status, blockMetaData } = command;

await this.elasticsearchService.update({
index: 'opinion',
id: hash,
refresh: 'wait_for',
body: {
doc: {
status: status,
blockMetaData: blockMetaData,
},
},
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BlockMetaData } from '../../../../models/block-meta-data';
import { Opinion } from '@indexer/models/opinion';

export class OpinionUpdatedCommandIndexer {
accountId: string;
opinion: Opinion;
constructor(data: Array<any>, public readonly blockMetaData: BlockMetaData) {
this.accountId = data[0].toString();
this.opinion = new Opinion(data[1].toHuman());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Injectable } from '@nestjs/common';
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { ElasticsearchService } from '@nestjs/elasticsearch';
import { OpinionUpdatedCommandIndexer } from './opinion-updated.command';

@Injectable()
@CommandHandler(OpinionUpdatedCommandIndexer)
export class OpinionUpdatedHandler
implements ICommandHandler<OpinionUpdatedCommandIndexer>
{
constructor(private readonly elasticsearchService: ElasticsearchService) {}

async execute(command: OpinionUpdatedCommandIndexer) {
const {
opinion: { info, id },
blockMetaData,
} = command;

await this.elasticsearchService.update({
index: 'opinion',
id: id,
refresh: 'wait_for',
body: {
doc: {
info: {
...info,
},
blockMetaData: blockMetaData,
},
},
});
}
}
16 changes: 16 additions & 0 deletions src/indexer/events/opinion/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export * from './commands/opinion-added/opinion-added.command';
export * from './commands/opinion-removed/opinion-removed.command';
export * from './commands/opinion-status-updated/opinion-status-updated.command';
export * from './commands/opinion-updated/opinion-updated.command';

import { OpinionAddedHandler } from './commands/opinion-added/opinion-added.handler';
import { OpinionRemovedHandler } from './commands/opinion-removed/opinion-removed.handler';
import { OpinionStatusUpdatedHandler } from './commands/opinion-status-updated/opinion-status-updated.handler';
import { OpinionUpdatedHandler } from './commands/opinion-updated/opinion-updated.handler';

export const OpinionCommandHandlers = [
OpinionAddedHandler,
OpinionRemovedHandler,
OpinionStatusUpdatedHandler,
OpinionUpdatedHandler,
];
2 changes: 2 additions & 0 deletions src/indexer/indexer.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ export class IndexerHandler
'menstrual-cycle-log',
'menstrual-subscription',
'health-professional',
'opinion',
'opinion-requestor',
];

for (const i of indices) {
Expand Down
4 changes: 4 additions & 0 deletions src/indexer/indexer.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { GeneticAnalysisOrderCommandHandlers } from './events/genetic-analysis-o
import { MenstrualCalendarCommandHandlers } from './events/menstrual-calendar';
import { MenstrualSubscriptionCommandHandlers } from './events/menstrual-subscription';
import { HealthProfessionalHandlers } from './events/health-professional';
import { OpinionCommandHandlers } from './events/opinion';
import { OpinionRequestorCommandHandlers } from './events/opinion-requestor';

// eslint-disable-next-line @typescript-eslint/no-var-requires
require('dotenv').config();
Expand All @@ -43,6 +45,8 @@ require('dotenv').config();
...MenstrualCalendarCommandHandlers,
...MenstrualSubscriptionCommandHandlers,
...HealthProfessionalHandlers,
...OpinionCommandHandlers,
...OpinionRequestorCommandHandlers,
],
})
export class IndexerModule {}
20 changes: 20 additions & 0 deletions src/indexer/indexer.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ import {
HealthProfessionalUnstakedCommandIndexer,
HealthProfessionalVerificationStatusCommandIndexer,
} from './events/health-professional';
import {
OpinionAddedCommandIndexer,
OpinionRemovedCommandIndexer,
OpinionStatusUpdatedCommandIndexer,
OpinionUpdatedCommandIndexer,
} from './events/opinion';
import {
OpinionRequestedCommandIndexer,
OpinionRequestorInfoUpdatedCommandIndexer,
} from './events/opinion-requestor';

export const eventRoutes = {
certifications: {
Expand Down Expand Up @@ -207,4 +217,14 @@ export const eventRoutes = {
HealthProfessionalUnstakedAmount:
HealthProfessionalUnstakedAmountCommandIndexer,
},
opinion: {
OpinionAdded: OpinionAddedCommandIndexer,
OpinionUpdated: OpinionUpdatedCommandIndexer,
OpinionRemoved: OpinionRemovedCommandIndexer,
OpinionStatusUpdated: OpinionStatusUpdatedCommandIndexer,
},
opinionRequestor: {
OpinionRequested: OpinionRequestedCommandIndexer,
OpinionRequestorInfoUpdated: OpinionRequestorInfoUpdatedCommandIndexer,
},
};
21 changes: 21 additions & 0 deletions src/indexer/models/opinion-requestor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { RequestorInfo } from './info';

export class OpinionRequestor {
constructor(data: any) {
this.id = data.id;
this.account_id = data.account_id;
this.info = new RequestorInfo(data.info);
this.created_at = data.created_at
? new Date(Number(String(data.created_at).split(',').join('')))
: null;
this.updated_at = data.updated_at
? new Date(Number(String(data.updated_at).split(',').join('')))
: null;
}

id: string;
account_id: string;
info: RequestorInfo;
created_at: Date;
updated_at: Date;
}
15 changes: 15 additions & 0 deletions src/indexer/models/opinion-requestor/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export class RequestorInfo {
constructor(data: any) {
this.category = data.category;
this.description = data.description;
this.genetic_data_ids = data.genetic_data_ids;
this.opinion_ids = data.opinion_ids;
this.myriad_url = data.myriad_url;
}

category: string;
description: string;
genetic_data_ids: string[];
opinion_ids: string[];
myriad_url: string;
}
19 changes: 19 additions & 0 deletions src/indexer/models/opinion/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { OpinionInfo } from './info';

export class Opinion {
constructor(data: any) {
this.id = data.id;
this.requestor_id = data.requestor_id;
this.professional_id = data.professional_id;
this.info = new OpinionInfo(data.info);
this.status = data.status;
this.created_at = data.created_at;
}

id: string;
requestor_id: string;
professional_id: string;
info: OpinionInfo;
status: string;
created_at: Date;
}
Loading

0 comments on commit 30a6067

Please sign in to comment.