Skip to content

Commit

Permalink
add header notification on user side and refactor admin side
Browse files Browse the repository at this point in the history
  • Loading branch information
bahram1249 committed Aug 7, 2024
1 parent e15ff47 commit 7511e86
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,16 @@ export class HeaderNotificationDto {
@IsOptional()
@AutoMap()
message?: string;

@MinLength(3)
@MaxLength(2048)
@IsOptional()
@AutoMap()
textColor?: string;

@MinLength(3)
@MaxLength(2048)
@IsOptional()
@AutoMap()
backgroundColor?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,75 @@ import { Setting } from '@rahino/database/models/core/setting.entity';
@Injectable()
export class HeaderNotificationService {
private readonly HEADER_NOTIFICATION_TEXT = 'HEADER_NOTIFICATION_TEXT';
private readonly HEADER_NOTIFICATION_TEXT_COLOR =
'HEADER_NOTIFICATION_TEXT_COLOR';
private readonly HEADER_NOTIFICATION_BACKGROUND_COLOR =
'HEADER_NOTIFICATION_BACKGROUND_COLOR';
constructor(@InjectModel(Setting) private repository: typeof Setting) {}

async findOne() {
let queryBuilder = new QueryOptionsBuilder().filter({
key: this.HEADER_NOTIFICATION_TEXT,
});
const result = await this.repository.findOne(queryBuilder.build());
const notificationText = await this.repository.findOne(
new QueryOptionsBuilder()
.filter({
key: this.HEADER_NOTIFICATION_TEXT,
})
.build(),
);
const notificationTextColor = await this.repository.findOne(
new QueryOptionsBuilder()
.filter({
key: this.HEADER_NOTIFICATION_TEXT_COLOR,
})
.build(),
);
const notificationBackgroundColor = await this.repository.findOne(
new QueryOptionsBuilder()
.filter({
key: this.HEADER_NOTIFICATION_BACKGROUND_COLOR,
})
.build(),
);
return {
result: result.value,
result: {
message: notificationText.value,
textColor: notificationTextColor.value,
backgroundColor: notificationBackgroundColor.value,
},
};
}

async update(dto: HeaderNotificationDto, user: User) {
let queryBuilder = new QueryOptionsBuilder().filter({
key: this.HEADER_NOTIFICATION_TEXT,
});
let result = await this.repository.findOne(queryBuilder.build());
result.value = dto.message;
result = await result.save();
let headerNotificationText = await this.repository.findOne(
new QueryOptionsBuilder()
.filter({
key: this.HEADER_NOTIFICATION_TEXT,
})
.build(),
);
headerNotificationText.value = dto.message;
headerNotificationText = await headerNotificationText.save();

let headerNotificationTextColor = await this.repository.findOne(
new QueryOptionsBuilder()
.filter({
key: this.HEADER_NOTIFICATION_TEXT_COLOR,
})
.build(),
);
headerNotificationTextColor.value = dto.textColor;
headerNotificationTextColor = await headerNotificationTextColor.save();

let headerNotificationBackgroundColor = await this.repository.findOne(
new QueryOptionsBuilder()
.filter({
key: this.HEADER_NOTIFICATION_BACKGROUND_COLOR,
})
.build(),
);
headerNotificationBackgroundColor.value = dto.backgroundColor;
headerNotificationBackgroundColor =
await headerNotificationBackgroundColor.save();

return {
result: 'ok',
};
Expand Down
2 changes: 2 additions & 0 deletions apps/e-commerce/src/e-commerce.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import { ProductVideoRemovalModule } from './product-video-removal/product-video
import { AdminNotificationModule } from './admin/notification/notification.module';
import { NotificationModule } from './user/notification/notification.module';
import { AdminHeaderNotificationModule } from './admin/header-notification/notification.module';
import { UserHeaderNotificationModule } from './user/header-notification/notification.module';

@Module({
imports: [
Expand Down Expand Up @@ -186,6 +187,7 @@ import { AdminHeaderNotificationModule } from './admin/header-notification/notif
AdminNotificationModule,
NotificationModule,
AdminHeaderNotificationModule,
UserHeaderNotificationModule,
],
providers: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
Controller,
Get,
HttpCode,
HttpStatus,
UseInterceptors,
} from '@nestjs/common';
import { JsonResponseTransformInterceptor } from '@rahino/response/interceptor';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { HeaderNotificationService } from './notification.service';

@ApiTags('Admin-HeaderNotifications')
@Controller({
path: '/api/ecommerce/user/headerNotifications',
version: ['1'],
})
export class HeaderNotificationController {
constructor(private service: HeaderNotificationService) {}

@UseInterceptors(JsonResponseTransformInterceptor)
@ApiOperation({ description: 'get header notification' })
@Get('/')
@HttpCode(HttpStatus.OK)
async findOne() {
return await this.service.findOne();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { HeaderNotificationController } from './notification.controller';
import { HeaderNotificationService } from './notification.service';
import { SequelizeModule } from '@nestjs/sequelize';
import { Setting } from '@rahino/database/models/core/setting.entity';

@Module({
imports: [SequelizeModule.forFeature([Setting])],
controllers: [HeaderNotificationController],
providers: [HeaderNotificationService],
})
export class UserHeaderNotificationModule implements NestModule {
configure(consumer: MiddlewareConsumer) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { QueryOptionsBuilder } from '@rahino/query-filter/sequelize-query-builder';
import * as _ from 'lodash';
import { Setting } from '@rahino/database/models/core/setting.entity';

@Injectable()
export class HeaderNotificationService {
private readonly HEADER_NOTIFICATION_TEXT = 'HEADER_NOTIFICATION_TEXT';
private readonly HEADER_NOTIFICATION_TEXT_COLOR =
'HEADER_NOTIFICATION_TEXT_COLOR';
private readonly HEADER_NOTIFICATION_BACKGROUND_COLOR =
'HEADER_NOTIFICATION_BACKGROUND_COLOR';
constructor(@InjectModel(Setting) private repository: typeof Setting) {}

async findOne() {
const notificationText = await this.repository.findOne(
new QueryOptionsBuilder()
.filter({
key: this.HEADER_NOTIFICATION_TEXT,
})
.build(),
);
const notificationTextColor = await this.repository.findOne(
new QueryOptionsBuilder()
.filter({
key: this.HEADER_NOTIFICATION_TEXT_COLOR,
})
.build(),
);
const notificationBackgroundColor = await this.repository.findOne(
new QueryOptionsBuilder()
.filter({
key: this.HEADER_NOTIFICATION_BACKGROUND_COLOR,
})
.build(),
);
return {
result: {
message: notificationText.value,
textColor: notificationTextColor.value,
backgroundColor: notificationBackgroundColor.value,
},
};
}
}
21 changes: 21 additions & 0 deletions apps/main/src/sql/core-v1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7999,6 +7999,27 @@ END
GO


IF NOT EXISTS ( SELECT 1 FROM Migrations WHERE version = 'ec-headernotification-Data-v2'
)
AND EXISTS (
SELECT 1 FROM Settings
WHERE ([key] = 'SITE_NAME' AND [value] IN ('ECommerce'))
)

BEGIN

INSERT INTO Settings([key], [value], [type], createdAt, updatedAt)
SELECT N'HEADER_NOTIFICATION_TEXT', NULL, N'string', getdate(), getdate()


INSERT INTO Migrations(version, createdAt, updatedAt)
SELECT 'ec-headernotification-Data-v2', GETDATE(), GETDATE()
END

GO



-- auth/admin/users
IF NOT EXISTS ((SELECT 1 FROM Migrations WHERE version = 'CORE-Permissions-Data-v1'
))
Expand Down

0 comments on commit 7511e86

Please sign in to comment.