Skip to content

Commit

Permalink
update on create
Browse files Browse the repository at this point in the history
  • Loading branch information
arunas-smala committed Jun 10, 2024
1 parent d0db438 commit e5bab09
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion services/subscriptions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
Table,
throwNoRightsError,
UserAuthMeta,
EntityChangedParams,
} from '../types';
import { LKS_SRID } from '../utils';
import { App } from './apps.service';
Expand All @@ -32,6 +33,10 @@ interface Fields extends CommonFields {
frequency: Frequency;
active: boolean;
geomWithBuffer?: FeatureCollection;
eventsCount?: {
allTime: number;
new: number;
};
}

interface Populates extends CommonPopulates {
Expand All @@ -46,7 +51,13 @@ export type Subscription<

@Service({
name: 'subscriptions',
mixins: [DbConnection({ collection: 'subscriptions' }), PostgisMixin({ srid: LKS_SRID })],
mixins: [
DbConnection({
collection: 'subscriptions',
entityChangedOldEntity: true,
}),
PostgisMixin({ srid: LKS_SRID }),
],
settings: {
fields: {
id: {
Expand Down Expand Up @@ -362,6 +373,39 @@ export default class SubscriptionsService extends moleculer.Service {
return countBySubscriptions;
}

@Event()
async 'subscriptions.*'(ctx: Context<EntityChangedParams<Subscription>>) {
const type = ctx.params.type;
const subscription = ctx.params.data;
const subscriptionOld = ctx.params.oldData;
const id = subscription.id;

if (!id) return;
if (subscription.geom === subscriptionOld?.geom) return;

switch (type) {
case 'create':
case 'update':
case 'replace':
const eventsCounts = await this.actions.getEventsCount({
id,
mapping: true,
});

await this.updateEntity(
ctx,
{
id,
eventsCount: eventsCounts[id],
},
{
permissive: true,
},
);
break;
}
}

@Event()
async 'integrations.sync.finished'(ctx: Context) {
const allSubscriptions: Array<Subscription<null, 'id'>> = await this.findEntities(ctx, {
Expand Down

0 comments on commit e5bab09

Please sign in to comment.