-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add notification setting service
- Loading branch information
1 parent
6679b6c
commit e6d8e8c
Showing
6 changed files
with
147 additions
and
8 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
11 changes: 9 additions & 2 deletions
11
...c/lib/user-notification/user-notification-setting/user-notification-setting.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 |
---|---|---|
@@ -1,4 +1,11 @@ | ||
import { Controller } from '@nestjs/common'; | ||
import { CrudController } from '../../core/crud'; | ||
import { UserNotificationSetting } from './user-notification-setting.entity'; | ||
import { UserNotificationSettingService } from './user-notification-setting.service'; | ||
|
||
@Controller('user-notification-setting') | ||
export class UserNotificationSettingController {} | ||
@Controller('/user-notification-setting') | ||
export class UserNotificationSettingController extends CrudController<UserNotificationSetting> { | ||
constructor(private readonly userNotificationSettingService: UserNotificationSettingService) { | ||
super(userNotificationSettingService); | ||
} | ||
} |
16 changes: 14 additions & 2 deletions
16
...e/src/lib/user-notification/user-notification-setting/user-notification-setting.module.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,9 +1,21 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CqrsModule } from '@nestjs/cqrs'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { MikroOrmModule } from '@mikro-orm/nestjs'; | ||
import { RolePermissionModule } from '../../role-permission/role-permission.module'; | ||
import { UserNotificationSettingService } from './user-notification-setting.service'; | ||
import { UserNotificationSettingController } from './user-notification-setting.controller'; | ||
import { UserNotificationSetting } from './user-notification-setting.entity'; | ||
import { TypeOrmUserNotificationSettingRepository } from './repository/type-orm-user-notification-setting.repository'; | ||
|
||
@Module({ | ||
providers: [UserNotificationSettingService], | ||
controllers: [UserNotificationSettingController] | ||
imports: [ | ||
TypeOrmModule.forFeature([UserNotificationSetting]), | ||
MikroOrmModule.forFeature([UserNotificationSetting]), | ||
RolePermissionModule, | ||
CqrsModule | ||
], | ||
controllers: [UserNotificationSettingController], | ||
providers: [UserNotificationSettingService, TypeOrmUserNotificationSettingRepository] | ||
}) | ||
export class UserNotificationSettingModule {} |
37 changes: 35 additions & 2 deletions
37
.../src/lib/user-notification/user-notification-setting/user-notification-setting.service.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,4 +1,37 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; | ||
import { IUserNotificationSetting } from '@gauzy/contracts'; | ||
import { TenantAwareCrudService } from '../../core/crud'; | ||
import { RequestContext } from '../../core/context'; | ||
import { UserNotificationSetting } from './user-notification-setting.entity'; | ||
import { TypeOrmUserNotificationSettingRepository } from './repository/type-orm-user-notification-setting.repository'; | ||
import { MikroOrmUserNotificationSettingRepository } from './repository/mikro-orm-user-notification-setting.repository'; | ||
|
||
@Injectable() | ||
export class UserNotificationSettingService {} | ||
export class UserNotificationSettingService extends TenantAwareCrudService<UserNotificationSetting> { | ||
constructor( | ||
readonly typeOrmUserNotificationSettingRepository: TypeOrmUserNotificationSettingRepository, | ||
readonly mikroOrmUserNotificationSettingRepository: MikroOrmUserNotificationSettingRepository | ||
) { | ||
super(typeOrmUserNotificationSettingRepository, mikroOrmUserNotificationSettingRepository); | ||
} | ||
|
||
/** | ||
* Creates an user notification setting record | ||
* | ||
* @param {IUserNotificationSetting} input - The input data for creating a notification setting | ||
* @returns {Promise<UserNotificationSetting>} The created notification setting | ||
*/ | ||
async create(input: IUserNotificationSetting): Promise<UserNotificationSetting> { | ||
try { | ||
const userId = RequestContext.currentUserId(); | ||
const tenantId = RequestContext.currentTenantId() || input.tenantId; | ||
|
||
return super.create({ ...input, userId, tenantId }); | ||
} catch (error) { | ||
throw new HttpException( | ||
`Failed to create the notification setting: ${error.message}`, | ||
HttpStatus.BAD_REQUEST | ||
); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...c/lib/user-notification/user-notification-setting/user-notification-setting.subscriber.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,84 @@ | ||
import { EventSubscriber } from 'typeorm'; | ||
import { isBetterSqlite3, isSqlite } from '@gauzy/config'; | ||
import { BaseEntityEventSubscriber } from '../../core/entities/subscribers/base-entity-event.subscriber'; | ||
import { UserNotificationSetting } from './user-notification-setting.entity'; | ||
|
||
@EventSubscriber() | ||
export class UserNotificationSettingSubscriber extends BaseEntityEventSubscriber<UserNotificationSetting> { | ||
/** | ||
* Indicates that this subscriber only listen to UserNotificationSetting events. | ||
*/ | ||
listenTo() { | ||
return UserNotificationSetting; | ||
} | ||
|
||
/** | ||
* Called before an UserNotificationSetting entity is inserted or updated in the database. | ||
* This method prepares the entity for insertion or update by serializing the preferences property to a JSON string | ||
* for SQLite databases. | ||
* | ||
* @param entity The UserNotificationSetting entity that is about to be created or updated. | ||
* @returns {Promise<void>} A promise that resolves when the pre-creation or pre-update processing is complete. | ||
*/ | ||
private async serializePreferencesForSQLite(entity: UserNotificationSetting): Promise<void> { | ||
try { | ||
// Check if the database is SQLite | ||
if (isSqlite() || isBetterSqlite3()) { | ||
// serialize the `preferences` field if it's an object | ||
if (typeof entity.preferences === 'object') { | ||
entity.preferences = JSON.stringify(entity.preferences); | ||
} | ||
} | ||
} catch (error) { | ||
// Log the error and reset the data to an empty object if JSON parsing fails | ||
console.error(error); | ||
entity.preferences = '{}'; | ||
} | ||
} | ||
|
||
/** | ||
* Called before an UserNotificationSetting entity is inserted or created in the database. | ||
* This method prepares the entity for insertion by serializing the preferences property to a JSON string for SQLite DBs | ||
* | ||
* @param entity The UserNotificationSetting entity that is about to be created. | ||
* @returns {Promise<void>} A promise that resolves when the pre-insertion processing is complete. | ||
*/ | ||
async beforeEntityCreate(entity: UserNotificationSetting): Promise<void> { | ||
await this.serializePreferencesForSQLite(entity); | ||
} | ||
|
||
/** | ||
* Called before an UserNotificationSetting entity is updated in the database. | ||
* This method prepares the entity for update by serializing the preferences property to a JSON string | ||
* | ||
* @param entity The UserNotificationSetting entity that is about to be updated. | ||
* @returns {Promise<void>} A promise that resolves when the pre-update processing is complete. | ||
*/ | ||
async beforeEntityUpdate(entity: UserNotificationSetting): Promise<void> { | ||
await this.serializePreferencesForSQLite(entity); | ||
} | ||
|
||
/** | ||
* Handles the parsing of JSON data after the UserNotificationSetting entity is loaded from the database. | ||
* This function ensures that if the database is SQLite, the `preferences` field, stored as a JSON string, | ||
* is parsed back into a JavaScript object. | ||
* | ||
* @param {UserNotificationSetting} entity - The UserNotificationSetting entity that has been loaded from the database. | ||
* @returns {Promise<void>} A promise that resolves once the after-load processing is complete. | ||
*/ | ||
async afterEntityLoad(entity: UserNotificationSetting): Promise<void> { | ||
try { | ||
// Check if the database is SQLite | ||
if (isSqlite() || isBetterSqlite3()) { | ||
// Parse the `preferences` field if it's a string | ||
if (entity.preferences && typeof entity.preferences === 'string') { | ||
entity.preferences = JSON.parse(entity.preferences); | ||
} | ||
} | ||
} catch (error) { | ||
// Log the error and reset the data to an empty object if JSON parsing fails | ||
console.error('Error parsing JSON data in afterEntityLoad:', error); | ||
entity.preferences = {}; | ||
} | ||
} | ||
} |