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
52 changes: 52 additions & 0 deletions services/newsfeed.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';
import { isEmpty } from 'lodash';
import moleculer, { Context } from 'moleculer';
import { Action, Service } from 'moleculer-decorators';
import { intersectsQuery } from 'moleculer-postgis';
import { UserAuthMeta } from '../types';
import { parseToJsonIfNeeded } from '../utils';
import { Event } from './events.service';
import { Subscription } from './subscriptions.service';

@Service({
name: 'newsfeed',
})
export default class NewsfeedService extends moleculer.Service {
@Action({
rest: 'GET /',
})
async getNewsFeed(ctx: Context<any, UserAuthMeta>): Promise<any> {
LWangllix marked this conversation as resolved.
Show resolved Hide resolved
const { user } = ctx.meta;

ctx.params.query = parseToJsonIfNeeded(ctx.params.query) || {};

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

if (!isEmpty(subscriptions)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newsfeed'as turi reikalaut subscriptionu. Tai siulyciau igyvendint tiesiog tuscio masyvo grazinima (puslapiavimo formatu)

const subscriptionQuery = subscriptions.map((subscription) => ({
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;
}
}

const events: Event[] = await ctx.call('events.list', {
...ctx,
query: { ...ctx.params.query },
});

return events;
}
}
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