diff --git a/docsfera.json b/docsfera.json index 0922da4..381bdfe 100644 --- a/docsfera.json +++ b/docsfera.json @@ -5371,6 +5371,92 @@ ] } }, + "/customers/{id}/theme": { + "post": { + "operationId": "CustomersController_saveCustomertheme", + "parameters": [ + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CustomerThemeRequest" + } + } + } + }, + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CustomerThemeResponse" + } + } + } + }, + "201": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CustomerThemeResponse" + } + } + } + } + }, + "tags": [ + "Customers" + ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + } + ] + }, + "get": { + "operationId": "CustomersController_getCustomerTheme", + "parameters": [ + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CustomerThemeResponse" + } + } + } + } + }, + "tags": [ + "Customers" + ] + } + }, "/customers/token": { "get": { "operationId": "CustomersController_getCustomerToken", @@ -8221,6 +8307,63 @@ "links" ] }, + "CustomerThemeRequest": { + "type": "object", + "properties": { + "backgroundColor": { + "type": "string" + }, + "textColor": { + "type": "string" + }, + "logoWhite": { + "type": "string" + }, + "logoBlack": { + "type": "string" + } + }, + "required": [ + "backgroundColor", + "textColor", + "logoWhite", + "logoBlack" + ] + }, + "CustomerTheme": { + "type": "object", + "properties": { + "backgroundColor": { + "type": "string" + }, + "textColor": { + "type": "string" + }, + "logoWhite": { + "type": "string" + }, + "logoBlack": { + "type": "string" + } + }, + "required": [ + "backgroundColor", + "textColor", + "logoWhite", + "logoBlack" + ] + }, + "CustomerThemeResponse": { + "type": "object", + "properties": { + "theme": { + "$ref": "#/components/schemas/CustomerTheme" + } + }, + "required": [ + "theme" + ] + }, "CustomerLinkRequest": { "type": "object", "properties": { @@ -8238,4 +8381,4 @@ } } } -} +} \ No newline at end of file diff --git a/src/modules/customers/customers.controller.ts b/src/modules/customers/customers.controller.ts index 04073ec..4268e6a 100644 --- a/src/modules/customers/customers.controller.ts +++ b/src/modules/customers/customers.controller.ts @@ -5,9 +5,11 @@ import { Controller, Get, HttpCode, + HttpException, HttpStatus, Inject, Param, + Post, Put, Query, UseFilters, @@ -18,17 +20,16 @@ import { Authenticated, RequireAllPermissions, } from 'src/decorators/authentication.decorator'; -import { ApiInternalOnlyEndpoint } from 'src/decorators/swagger.decorator'; import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter'; import { CustomersService } from './customers.service'; -import { CustomerLinkRequest, CustomerLinksResponse } from './dtos/customers'; +import { CustomerLinkRequest, CustomerLinksResponse, CustomerThemeRequest, CustomerThemeResponse } from './dtos/customers'; import { RequestUser, User } from 'src/decorators/user.decorator'; import type { StringValue } from 'ms'; import { PackTheMetadata } from 'src/utils/ PackTheMetadata'; +import ErrorCodes from 'src/utils/errorCodes'; @ApiTags('Customers') @Controller('customers') -@Authenticated() @UseFilters(GrpcToHttpExceptionFilter) export class CustomersController { logger: DadosferaLogger; @@ -42,6 +43,7 @@ export class CustomersController { } @Get(':id/links') + @Authenticated() @ApiOkResponse({ type: CustomerLinksResponse }) async getCustomerLinks(@Param('id') id: string) { this.logger.info('getCustomerLinks', { id }); @@ -49,7 +51,41 @@ export class CustomersController { return { links }; } + @Post(':id/theme') + @Authenticated() + @RequireAllPermissions(PERMISSIONS_GROUPS.USERS.permissions.ADMIN) + @ApiOkResponse({ type: CustomerThemeResponse }) + async saveCustomertheme(@Param('id') id: string, @Body() data: CustomerThemeRequest) { + this.logger.info('saveCustomertheme', JSON.stringify({ + id, theme: data + })); + const theme = await this.customersService.createThemeByCustomer(id, data); + + return theme; + } + + @Get(':id/theme') + @ApiOkResponse({ type: CustomerThemeResponse }) + async getCustomerTheme(@Param('id') id: string) { + this.logger.info('getCustomerTheme with id' + id); + + try { + const data = await this.customersService.getThemeByCustomer(id); + this.logger.info('Success - getCustomerTheme with id'+ id); + return data; + }catch (err) { + if (err.details === ErrorCodes.CUSTOMER.NOT_FOUND) { + this.logger.error('Error - getCustomerTheme - Expect CUSTOMER.NOT_FOUND'); + throw new HttpException(err.details, HttpStatus.NOT_FOUND); + } else { + this.logger.error('Error - getCustomerTheme Unknown Error:' + err?.message); + return { theme: null }; + }; + } + } + @Put(':id/links') + @Authenticated() @RequireAllPermissions(PERMISSIONS_GROUPS.USERS.permissions.ADMIN) @ApiOkResponse() @HttpCode(HttpStatus.OK) @@ -63,6 +99,7 @@ export class CustomersController { } @Get('token') + @Authenticated() @RequireAllPermissions(PERMISSIONS_GROUPS.AUTH.permissions.GENERATE_TOKEN) @ApiProduces('text/plain') async getCustomerToken( @@ -79,6 +116,7 @@ export class CustomersController { } @Get('monitoring-dashboard') + @Authenticated() @RequireAllPermissions( PERMISSIONS_GROUPS.CUSTOMER.permissions.MONITORING_DASHBOARD, ) diff --git a/src/modules/customers/customers.service.ts b/src/modules/customers/customers.service.ts index 5102db5..5dc43f3 100644 --- a/src/modules/customers/customers.service.ts +++ b/src/modules/customers/customers.service.ts @@ -28,19 +28,25 @@ import { } from '@dadosfera/protospack-v2/dist/lib/PipelineV2'; import { Metadata } from '@grpc/grpc-js'; import { PipelinesClientConfiguration } from '../pipelinesV2/pipelines-client'; +import { CustomerThemeRequest, CustomerThemeResponse } from './dtos/customers'; +import DadosferaLogger from '@dadosfera/dadosfera-logs'; // This function will accept any string, which may result in a bug. @Injectable() export class CustomersService implements OnModuleInit { private customerService: CustomersProtoService; - + private logger: DadosferaLogger; private pipelineReadService: ReadService.PipelineV2ReadService; constructor( @Inject(DucClient.name) private readonly grpcClient: ClientGrpc, @Inject(PipelinesClientConfiguration.name) private readonly pipelinesGrpcClient: ClientGrpc, - ) {} + @Inject(DadosferaLogger) + dadosferaLogger: DadosferaLogger, + ) { + this.logger = dadosferaLogger.logger; + } onModuleInit() { this.customerService = this.grpcClient.getService( @@ -84,6 +90,48 @@ export class CustomersService implements OnModuleInit { } } + async createThemeByCustomer(id: string, theme: CustomerThemeRequest): Promise { + if (!id) { + this.logger.error('Error - saveCustomertheme - not found id:' + id); + throw new HttpException(null, HttpStatus.BAD_REQUEST); + } + + try { + const result = await firstValueFrom( + this.customerService.CustomerCreateTheme({ + customerId: id, + theme + }), + ); + return { theme: result }; + } catch (err) { + if (err.details === ErrorCodes.CUSTOMER.NOT_FOUND) { + this.logger.error('Error - saveCustomertheme - Expect CUSTOMER.NOT_FOUND'); + throw new HttpException(err.details, HttpStatus.NOT_FOUND); + } else { + this.logger.error('Error - saveCustomertheme Unknown Error:' + err?.message); + throw err + }; + } + } + + async getThemeByCustomer(id: string): Promise { + if (!id) { + this.logger.error('Error - getCustomerTheme - not found id:' + id); + throw new HttpException(null, HttpStatus.BAD_REQUEST); + } + + const result = await firstValueFrom( + this.customerService.CustomerGetTheme({ + id + }), + ); + + return { + theme: result + } + } + async generateToken( expiresIn = '30m', data: { customerId: string; userId: string; customerName: string }, diff --git a/src/modules/customers/dtos/customers.ts b/src/modules/customers/dtos/customers.ts index c3b3070..e26fc94 100644 --- a/src/modules/customers/dtos/customers.ts +++ b/src/modules/customers/dtos/customers.ts @@ -1,4 +1,4 @@ -import { Link } from '@dadosfera/protospack-v2/dist/lib/Duc/interfaces/entities'; +import { Link, Theme } from '@dadosfera/protospack-v2/dist/lib/Duc/interfaces/entities'; import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; export class CustomerLink implements Link { @@ -20,3 +20,30 @@ export class CustomerLinksResponse { @ApiProperty({ type: [CustomerLink] }) links: CustomerLink[]; } + +export class CustomerTheme implements Theme { + @ApiProperty() + backgroundColor: string; + @ApiProperty() + textColor: string; + @ApiProperty() + logoWhite: string; + @ApiProperty() + logoBlack: string; +} + +export class CustomerThemeResponse { + @ApiProperty() + theme: CustomerTheme; +} + +export class CustomerThemeRequest { + @ApiProperty() + backgroundColor: string; + @ApiProperty() + textColor: string; + @ApiProperty() + logoWhite: string; + @ApiProperty() + logoBlack: string; +}