Skip to content

Commit

Permalink
feat: add notification setting service
Browse files Browse the repository at this point in the history
  • Loading branch information
GloireMutaliko21 committed Jan 31, 2025
1 parent 6679b6c commit e6d8e8c
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/core/src/lib/core/entities/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,4 @@ export * from '../../time-tracking/screenshot/screenshot.subscriber';
export * from '../../time-tracking/timesheet/timesheet.subscriber';
export * from '../../time-tracking/time-slot/time-slot.subscriber';
export * from '../../user/user.subscriber';
export * from '../../user-notification/user-notification-setting/user-notification-setting.subscriber';
6 changes: 4 additions & 2 deletions packages/core/src/lib/core/entities/subscribers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ import {
TimeOffRequestSubscriber,
TimesheetSubscriber,
TimeSlotSubscriber,
UserSubscriber
UserSubscriber,
UserNotificationSettingSubscriber
} from '../internal';
import { TenantOrganizationBaseEntityEventSubscriber } from './tenant-organization-base-entity.subscriber';

Expand Down Expand Up @@ -102,5 +103,6 @@ export const coreSubscribers = [
TimeOffRequestSubscriber,
TimesheetSubscriber,
TimeSlotSubscriber,
UserSubscriber
UserSubscriber,
UserNotificationSettingSubscriber
];
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);
}
}
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 {}
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
);
}
}
}
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 = {};
}
}
}

0 comments on commit e6d8e8c

Please sign in to comment.