Skip to content

Commit

Permalink
refactor: update device
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingcedru committed Nov 28, 2024
1 parent 2717eb3 commit 3afaa09
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions apps/drec-api/src/pods/device/device.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,10 @@ export class DeviceController {
@Param('externalId') externalId: string,
@Body() deviceToUpdate: UpdateDeviceDTO,
): Promise<DeviceDTO> {
console.log(deviceToUpdate);
this.logger.verbose(`With in update`);
const org = await this.organizationService.findOne(deviceToUpdate.organizationId);
console.log(org)
if (user.role === Role.ApiUser) {
if (
user.api_user_id != org.api_user_id ||
Expand All @@ -493,6 +495,7 @@ export class DeviceController {
});
}
user.organizationId = deviceToUpdate.organizationId;
console.log(user.organizationId)
}

if (user.role != Role.Admin && user.organizationId != org.id) {
Expand Down
5 changes: 3 additions & 2 deletions apps/drec-api/src/pods/device/dto/new-device.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { OffTaker, FuelCode, DevicetypeCode } from '../../../utils/enums';
import { DeviceDescription, IDevice } from '../../../models';
import { Exclude, Transform } from 'class-transformer';
import { IsValidCommissioningDate } from '../../../validations/commissioning-date.validator';
import { Trim } from '../../../transformers/trim-string';

export class NewDeviceDTO
implements
Expand All @@ -25,7 +26,7 @@ export class NewDeviceDTO
>
{
@ApiProperty()
@Transform((value, obj) => obj.externalId?.trim())
@Trim()
@IsNotEmpty({ message: 'externalId should not be empty' })
@IsString()
@Matches(/^[a-zA-Z\d\-_\s]+$/, {
Expand Down Expand Up @@ -191,7 +192,7 @@ export class NewDeviceDTO
@ApiProperty({ default: '1.0' })
@IsString()
@IsOptional()
@Transform(({ value }) => value?.trim() || '')
@Trim()
@ValidateIf((o) => o.version === null || o.version === undefined)
version = '1.0';

Expand Down
4 changes: 2 additions & 2 deletions apps/drec-api/src/pods/device/dto/update-device.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { OffTaker, FuelCode, DevicetypeCode } from '../../../utils/enums';
import { IDevice } from '../../../models';
import { Exclude, Transform } from 'class-transformer';
import { IsValidCommissioningDate } from '../../../validations/commissioning-date.validator';
import { Trim } from '../../../transformers/trim-string';
export class UpdateDeviceDTO
implements
Omit<
Expand All @@ -29,7 +30,7 @@ export class UpdateDeviceDTO
{
@ApiProperty()
@IsOptional()
@Transform((value, obj) => obj.externalId?.trim())
@Trim()
@IsString({message: "externalId should not be empty"})
@Matches(/^[a-zA-Z\d\-_\s]+$/, {
message:
Expand Down Expand Up @@ -185,6 +186,5 @@ export class UpdateDeviceDTO
@IsOptional()
IREC_ID?: string;

@IsOptional()
organizationId?: number;
}
5 changes: 5 additions & 0 deletions apps/drec-api/src/transformers/trim-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { applyDecorators } from '@nestjs/common';
import { Transform } from 'class-transformer';

export const Trim = (): PropertyDecorator =>
applyDecorators(Transform((value?: string) => value?.trim()));

0 comments on commit 3afaa09

Please sign in to comment.