Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter events by user info #22

Merged
merged 20 commits into from
Feb 2, 2024
19 changes: 19 additions & 0 deletions database/migrations/20240129232022_addAppsToUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema.alterTable('users', (table) => {
table.jsonb('apps');
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.alterTable('users', (table) => {
table.dropColumn('apps');
});
};
19 changes: 19 additions & 0 deletions database/migrations/20240201152282_removeAppsFromUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema.alterTable('users', (table) => {
table.dropColumn('apps');
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.alterTable('users', (table) => {
table.jsonb('apps');
});
};
11 changes: 2 additions & 9 deletions services/api.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { User } from '@sentry/types';
import pick from 'lodash/pick';
import moleculer, { Context, Errors } from 'moleculer';
import { Action, Method, Service } from 'moleculer-decorators';
import ApiGateway from 'moleculer-web';
import {
AppAuthMeta,
EndpointType,
RequestMessage,
UserAuthMeta,
throwUnauthorizedError,
} from '../types';
import { App } from './apps.service';
import { EndpointType, RequestMessage, throwUnauthorizedError, UserAuthMeta } from '../types';
import { UserType } from './users.service';
import { User } from '@sentry/types';

@Service({
name: 'api',
Expand Down
6 changes: 3 additions & 3 deletions services/events.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { Service } from 'moleculer-decorators';
import PostgisMixin from 'moleculer-postgis';
import DbConnection from '../mixins/database.mixin';
import {
COMMON_FIELDS,
CommonFields,
CommonPopulates,
COMMON_DEFAULT_SCOPES,
COMMON_FIELDS,
COMMON_SCOPES,
EndpointType,
CommonPopulates,
Table,
CommonFields,
} from '../types';
import { App } from './apps.service';

Expand Down
89 changes: 89 additions & 0 deletions services/newsfeed.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict';
import { isEmpty } from 'lodash';
import moleculer, { Context } from 'moleculer';
import { Action, Method, Service } from 'moleculer-decorators';
import { intersectsQuery } from 'moleculer-postgis';
import { UserAuthMeta } from '../types';
import { parseToJsonIfNeeded } from '../utils';
import { Subscription } from './subscriptions.service';

@Service({
name: 'newsfeed',

hooks: {
before: {
list: ['applyFilters'],
find: ['applyFilters'],
get: ['applyFilters'],
resolve: ['applyFilters'],
},
},
})
export default class NewsfeedService extends moleculer.Service {
@Action({
rest: 'GET /',
})
async list(ctx: Context<any, UserAuthMeta>) {
return ctx.call('events.list', {
...ctx.params,
});
}

@Action()
async find(ctx: Context<any, UserAuthMeta>) {
return ctx.call('events.find', {
...ctx.params,
});
}

@Action()
async get(ctx: Context<any, UserAuthMeta>) {
const { id } = ctx.params;
return ctx.call('events.get', {
...ctx.params,
id,
});
}

@Action()
async resolve(ctx: Context<any, UserAuthMeta>) {
const { id } = ctx.params;
return ctx.call('events.resolve', {
...ctx.params,
id,
});
}

@Method
async applyFilters(ctx: Context<any, UserAuthMeta>) {
ctx.params.query = parseToJsonIfNeeded(ctx.params.query) || {};
const { user } = ctx.meta;

const subscriptions: Subscription[] = await ctx.call('subscriptions.find', {
query: {
user: user.id,
active: true,
},
populate: ['geom'],
});

if (isEmpty(subscriptions)) {
ctx.params.query.$or = { app: { $in: [] } };
return ctx;
}

const subscriptionQuery = subscriptions.map((subscription) => ({
...(!isEmpty(subscription.apps) && { app: { $in: subscription.apps } }),
$raw: intersectsQuery('geom', subscription.geom, 3346),
}));

if (ctx?.params?.query?.$or) {
ctx.params.query.$and = [ctx?.params?.query?.$or, { $or: subscriptionQuery }];
delete ctx?.params?.query?.$or;
} else {
ctx.params.query.$or = subscriptionQuery;
}

return ctx;
}
}
LWangllix marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions services/subscriptions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const sender = 'noreply@biip.lt';
interface Fields extends CommonFields {
user: User['id'];
apps: number[];
geom: any;
frequency: Frequency;
active: boolean;
lastSent: Date;
Expand Down
7 changes: 3 additions & 4 deletions services/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import { Action, Event, Service } from 'moleculer-decorators';

import DbConnection from '../mixins/database.mixin';
import {
COMMON_FIELDS,
BaseModelInterface,
COMMON_DEFAULT_SCOPES,
COMMON_FIELDS,
COMMON_SCOPES,
FieldHookCallback,
BaseModelInterface,
EndpointType,
FieldHookCallback,
throwNotFoundError,
UserAuthMeta,
} from '../types';
import PostgisMixin from 'moleculer-postgis';

export enum UserType {
ADMIN = 'ADMIN',
Expand Down
Loading