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

9 prenumeratos tvarkymas #28

Merged
merged 7 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import pick from 'lodash/pick';
import moleculer, { Context, Errors } from 'moleculer';
import { Action, Method, Service } from 'moleculer-decorators';
import ApiGateway from 'moleculer-web';
import { EndpointType, RequestMessage, throwUnauthorizedError, UserAuthMeta } from '../types';
import { UserType } from './users.service';
import {
EndpointType,
RequestMessage,
throwUnauthorizedError,
UserAuthMeta,
UserType,
} from '../types';

@Service({
name: 'api',
Expand Down
19 changes: 4 additions & 15 deletions services/apps.service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
'use strict';

import moleculer, { Context } from 'moleculer';
import { Action, Method, Service } from 'moleculer-decorators';

import { generateToken, verifyToken } from '../utils';
import moleculer from 'moleculer';
import { Service } from 'moleculer-decorators';
import DbConnection from '../mixins/database.mixin';
import {
COMMON_FIELDS,
COMMON_DEFAULT_SCOPES,
COMMON_SCOPES,
FieldHookCallback,
BaseModelInterface,
EndpointType,
CommonFields,
} from '../types';

export interface App extends BaseModelInterface {
export interface App extends CommonFields {
name: string;
key: string;
apiKey: string;
Expand All @@ -27,14 +24,12 @@ export const APPS = {

@Service({
name: 'apps',

mixins: [
DbConnection({
collection: 'apps',
rest: false,
}),
],

settings: {
fields: {
id: {
Expand All @@ -43,25 +38,19 @@ export const APPS = {
primaryKey: true,
secure: true,
},

key: 'string|required',
name: 'string|required',

apiKey: {
type: 'string',
hidden: true,
},

...COMMON_FIELDS,
},

scopes: {
...COMMON_SCOPES,
},

defaultScopes: [...COMMON_DEFAULT_SCOPES],
},

actions: {
create: {
auth: EndpointType.ADMIN,
Expand Down
4 changes: 2 additions & 2 deletions services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import moleculer, { Context, RestSchema } from 'moleculer';
import { Action, Event, Method, Service } from 'moleculer-decorators';

import authMixin from 'biip-auth-nodejs/mixin';
import { User, USERS_DEFAULT_SCOPES, UserType } from './users.service';
import { EndpointType, throwNotFoundError, UserAuthMeta } from '../types';
import { User } from './users.service';
import { EndpointType, throwNotFoundError, UserAuthMeta, UserType } from '../types';

@Service({
name: 'auth',
Expand Down
7 changes: 1 addition & 6 deletions services/events.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ export type Event<

@Service({
name: 'events',

mixins: [
DbConnection({
collection: 'events',
}),
PostgisMixin({ srid: 3346 }),
],

settings: {
fields: {
id: {
Expand All @@ -66,7 +64,7 @@ export type Event<
columnType: 'integer',
hidden: 'byDefault',
columnName: 'appId',
populate: 'apps.get',
populate: 'apps.resolve',
},
url: 'string',
body: 'string',
Expand All @@ -84,14 +82,11 @@ export type Event<
type: 'boolean',
default: false,
},

...COMMON_FIELDS,
},

scopes: {
...COMMON_SCOPES,
},

defaultScopes: [...COMMON_DEFAULT_SCOPES],
},
actions: {
Expand Down
1 change: 0 additions & 1 deletion services/seed.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import moleculer, { Context, ServiceBroker } from 'moleculer';
import { Action, Method, Service } from 'moleculer-decorators';
import { User } from './users.service';
import { APPS, App } from './apps.service';

@Service({
Expand Down
59 changes: 53 additions & 6 deletions services/subscriptions.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
'use strict';

import moleculer, { Context } from 'moleculer';
import { Action, Service } from 'moleculer-decorators';
import { Action, Method, Service } from 'moleculer-decorators';
import DbConnection from '../mixins/database.mixin';
import {
COMMON_DEFAULT_SCOPES,
COMMON_FIELDS,
COMMON_SCOPES,
CommonFields,
CommonPopulates,
EndpointType,
FieldHookCallback,
Frequency,
FrequencyLabel,
Table,
throwNoRightsError,
throwNotFoundError,
UserAuthMeta,
} from '../types';
import { User } from './users.service';
import { Event } from './events.service';
Expand All @@ -32,7 +37,6 @@ interface Fields extends CommonFields {
geom: any;
frequency: Frequency;
active: boolean;
lastSent: Date;
}

interface Populates extends CommonPopulates {
Expand Down Expand Up @@ -62,14 +66,24 @@ export type Subscription<
required: true,
columnType: 'integer',
columnName: 'userId',
populate: 'users.get',
immutable: true,
readonly: true,
populate: 'users.resolve',
onCreate: async ({ ctx }: FieldHookCallback) => ctx.meta.user?.id,
onUpdate: async ({ ctx, entity }: FieldHookCallback) => {
if (entity.userId !== ctx.meta.user?.id) {
return throwNoRightsError('Unauthorized');
}
return entity.userId;
},
},
apps: {
//apps subscribed to
type: 'array',
required: true,
items: { type: 'number' },
columnName: 'apps',
validate: 'validateApps',
populate(ctx: any, _values: any, items: Subscription[]) {
return Promise.all(
items.map((item: Subscription) => {
Expand All @@ -83,6 +97,7 @@ export type Subscription<
geom: {
type: 'any',
geom: true,
required: false, //TODO: should be true when map is ready
},
frequency: {
// email sending frequency
Expand All @@ -93,12 +108,31 @@ export type Subscription<
...COMMON_FIELDS,
},
scopes: {
user(query: any, ctx: Context<null, UserAuthMeta>, params: any) {
query.user = ctx.meta.user?.id;
return query;
},
...COMMON_SCOPES,
},
defaultScopes: [...COMMON_DEFAULT_SCOPES],
defaultScopes: [...COMMON_DEFAULT_SCOPES, 'user'],
},
actions: {
create: {
auth: EndpointType.USER,
},
update: {
auth: EndpointType.USER,
},
list: {
auth: EndpointType.USER,
},
find: {
auth: EndpointType.USER,
},
get: {
auth: EndpointType.USER,
},
remove: {
rest: null,
},
count: {
Expand Down Expand Up @@ -171,7 +205,6 @@ export default class SubscriptionsService extends moleculer.Service {
event_content: truncateString(e.body, 500),
url: e.url,
}));

const content = {
From: sender,
To: sub.user.email,
Expand All @@ -188,12 +221,26 @@ export default class SubscriptionsService extends moleculer.Service {
}
}

@Method
async validateApps({ ctx, value, entity }: FieldHookCallback) {
const apps: App[] = await ctx.call('apps.find', {
query: {
id: { $in: value },
},
});
const ids = apps.map((app: App) => app.id);
const diff = value.filter((id: number) => !ids.includes(id));
if (apps.length !== value.length) {
return `Invalid app ids [${diff.toString()}]`;
}
return true;
}

created() {
if (emailCanBeSent()) {
if (!process.env.POSTMARK_KEY) {
this.broker.fatal('POSTMARK is not configured');
}

this.client = new ServerClient(process.env.POSTMARK_KEY);
} else {
this.client = {
Expand Down
Loading
Loading