-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(notification-service): add relation for notification user
SFO-0
- Loading branch information
1 parent
2b75cd9
commit aa1b07d
Showing
9 changed files
with
251 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
135 changes: 135 additions & 0 deletions
135
services/notification-service/src/controllers/notification-notification-user.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
services/notification-service/src/controllers/notification-user-notification.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
services/notification-service/src/enums/permission-key.enum.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 19 additions & 3 deletions
22
services/notification-service/src/repositories/notification-user.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |
29 changes: 26 additions & 3 deletions
29
services/notification-service/src/repositories/notification.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |