Skip to content

Commit

Permalink
feat(notification-service): add relation for notification user
Browse files Browse the repository at this point in the history
SFO-0
  • Loading branch information
samarpan-b committed Nov 3, 2020
1 parent 2b75cd9 commit aa1b07d
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 14 deletions.
2 changes: 1 addition & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
},
parserPreset: {
parserOpts: {
issuePrefixes: ['SFO-'],
issuePrefixes: ['SFO-', '#', 'RD-', 'RPMS-', 'RFIT-'],
},
},
};
2 changes: 2 additions & 0 deletions services/notification-service/src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './pubnub-notification.controller';
export * from './notification-user.controller';
export * from './notification.controller';
export * from './notification-user-notification.controller';
export * from './notification-notification-user.controller';
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import {
Count,
CountSchema,
Filter,
repository,
Where,
} from '@loopback/repository';
import {
del,
get,
getModelSchemaRef,
getWhereSchemaFor,
param,
patch,
post,
requestBody,
} from '@loopback/rest';
import {authenticate, STRATEGY} from 'loopback4-authentication';
import {authorize} from 'loopback4-authorization';
import {PermissionKey} from '../enums/permission-key.enum';
import {Notification, NotificationUser} from '../models';
import {NotificationRepository} from '../repositories';

const basePath = '/notifications/{id}/notification-users';

export class NotificationNotificationUserController {
constructor(
@repository(NotificationRepository)
protected notificationRepository: NotificationRepository,
) {}

@authenticate(STRATEGY.BEARER)
@authorize([PermissionKey.ViewNotification])
@get(basePath, {
responses: {
'200': {
description: 'Array of Notification has many NotificationUser',
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(NotificationUser),
},
},
},
},
},
})
async find(
@param.path.string('id') id: string,
@param.query.object('filter') filter?: Filter<NotificationUser>,
): Promise<NotificationUser[]> {
return this.notificationRepository.notificationUsers(id).find(filter);
}

@authenticate(STRATEGY.BEARER)
@authorize([PermissionKey.CreateNotification])
@post(basePath, {
responses: {
'200': {
description: 'Notification model instance',
content: {
'application/json': {
schema: getModelSchemaRef(NotificationUser),
},
},
},
},
})
async create(
@param.path.string('id') id: typeof Notification.prototype.id,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(NotificationUser, {
title: 'NewNotificationUserInNotification',
exclude: ['id'],
optional: ['notificationId'],
}),
},
},
})
notificationUser: Omit<NotificationUser, 'id'>,
): Promise<NotificationUser> {
return this.notificationRepository
.notificationUsers(id)
.create(notificationUser);
}

@authenticate(STRATEGY.BEARER)
@authorize([PermissionKey.UpdateNotification])
@patch(basePath, {
responses: {
'200': {
description: 'Notification.NotificationUser PATCH success count',
content: {'application/json': {schema: CountSchema}},
},
},
})
async patch(
@param.path.string('id') id: string,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(NotificationUser, {partial: true}),
},
},
})
notificationUser: Partial<NotificationUser>,
@param.query.object('where', getWhereSchemaFor(NotificationUser))
where?: Where<NotificationUser>,
): Promise<Count> {
return this.notificationRepository
.notificationUsers(id)
.patch(notificationUser, where);
}

@authenticate(STRATEGY.BEARER)
@authorize([PermissionKey.DeleteNotification])
@del(basePath, {
responses: {
'200': {
description: 'Notification.NotificationUser DELETE success count',
content: {'application/json': {schema: CountSchema}},
},
},
})
async delete(
@param.path.string('id') id: string,
@param.query.object('where', getWhereSchemaFor(NotificationUser))
where?: Where<NotificationUser>,
): Promise<Count> {
return this.notificationRepository.notificationUsers(id).delete(where);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {repository} from '@loopback/repository';
import {get, getModelSchemaRef, param} from '@loopback/rest';
import {authenticate, STRATEGY} from 'loopback4-authentication';
import {authorize} from 'loopback4-authorization';

import {PermissionKey} from '../enums/permission-key.enum';
import {Notification, NotificationUser} from '../models';
import {NotificationUserRepository} from '../repositories';

export class NotificationUserNotificationController {
constructor(
@repository(NotificationUserRepository)
public notificationUserRepository: NotificationUserRepository,
) {}

@authenticate(STRATEGY.BEARER)
@authorize([PermissionKey.ViewNotification])
@get('/notification-users/{id}/notification', {
responses: {
'200': {
description: 'Notification belonging to NotificationUser',
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(Notification),
},
},
},
},
},
})
async getNotification(
@param.path.string('id') id: typeof NotificationUser.prototype.id,
): Promise<Notification> {
return this.notificationUserRepository.notification(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const enum PermissionKey {
ViewNotification = 'ViewNotification',
CreateNotification = 'CreateNotification',
UpdateNotification = 'UpdateNotification',
DeleteNotification = 'DeleteNotification',
CanGetNotificationAccess = 'CanGetNotificationAccess',
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {model, property} from '@loopback/repository';
import {model, property, belongsTo} from '@loopback/repository';
import {BaseEntity} from '@sourceloop/core';
import {Notification} from './notification.model';

@model({
name: 'notification_users',
Expand All @@ -11,11 +12,14 @@ export class NotificationUser extends BaseEntity {
})
id?: string;

@property({
type: 'string',
required: true,
name: 'notification_id',
})
@belongsTo(
() => Notification,
{name: 'notification'},
{
name: 'notification_id',
required: true,
},
)
notificationId: string;

@property({
Expand All @@ -36,3 +40,10 @@ export class NotificationUser extends BaseEntity {
super(data);
}
}

export interface NotificationUserRelations {
notificaTION: Notification;
}

export type NotificationUserWithRelations = NotificationUser &
NotificationUserRelations;
12 changes: 11 additions & 1 deletion services/notification-service/src/models/notification.model.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {Entity, model, property} from '@loopback/repository';
import {Entity, model, property, hasMany} from '@loopback/repository';
import {
Message,
Receiver,
MessageType,
MessageOptions,
} from 'loopback4-notifications';
import {NotificationUser} from './notification-user.model';

@model({
name: 'notifications',
Expand Down Expand Up @@ -53,7 +54,16 @@ export class Notification extends Entity implements Message {
})
options?: MessageOptions;

@hasMany(() => NotificationUser, {keyTo: 'notificationId'})
notificationUsers: NotificationUser[];

constructor(data?: Partial<Notification>) {
super(data);
}
}

export interface NotificationRelations {
notificationUsers: NotificationUser[];
}

export type NotificationWithRelations = Notification & NotificationRelations;
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import {inject} from '@loopback/core';
import {juggler} from '@loopback/repository';
import {Getter, inject} from '@loopback/core';
import {BelongsToAccessor, juggler, repository} from '@loopback/repository';
import {DefaultSoftCrudRepository} from '@sourceloop/core';

import {NotificationUser} from '../models';
import {Notification, NotificationUser} from '../models';
import {NotifDbSourceName} from '../types';
import {NotificationRepository} from './notification.repository';

export class NotificationUserRepository extends DefaultSoftCrudRepository<
NotificationUser,
typeof NotificationUser.prototype.id
> {
public readonly notification: BelongsToAccessor<
Notification,
typeof NotificationUser.prototype.id
>;

constructor(
@inject(`datasources.${NotifDbSourceName}`)
dataSource: juggler.DataSource,
@repository.getter('NotificationRepository')
protected notificationRepositoryGetter: Getter<NotificationRepository>,
) {
super(NotificationUser, dataSource);
this.notification = this.createBelongsToAccessorFor(
'notification',
notificationRepositoryGetter,
);
this.registerInclusionResolver(
'notification',
this.notification.inclusionResolver,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
import {inject} from '@loopback/core';
import {DefaultCrudRepository, juggler} from '@loopback/repository';
import {Getter, inject} from '@loopback/core';
import {
DefaultCrudRepository,
HasManyRepositoryFactory,
juggler,
repository,
} from '@loopback/repository';

import {Notification} from '../models';
import {Notification, NotificationUser} from '../models';
import {NotifDbSourceName} from '../types';
import {NotificationUserRepository} from './notification-user.repository';

export class NotificationRepository extends DefaultCrudRepository<
Notification,
typeof Notification.prototype.id
> {
public readonly notificationUsers: HasManyRepositoryFactory<
NotificationUser,
typeof Notification.prototype.id
>;

constructor(
@inject(`datasources.${NotifDbSourceName}`)
dataSource: juggler.DataSource,
@repository.getter('NotificationUserRepository')
protected notificationUserRepositoryGetter: Getter<
NotificationUserRepository
>,
) {
super(Notification, dataSource);
this.notificationUsers = this.createHasManyRepositoryFactoryFor(
'notificationUsers',
notificationUserRepositoryGetter,
);
this.registerInclusionResolver(
'notificationUsers',
this.notificationUsers.inclusionResolver,
);
}
}

0 comments on commit aa1b07d

Please sign in to comment.