Commit 93e00654 authored by Lorenzo Trujillo Rojas's avatar Lorenzo Trujillo Rojas
Browse files

Merge branch 'main' into 'main'

Agregando endpoint para actualizar un place

See merge request !41
parents f3b6e95e ff4b1694
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
import { PartialType } from '@nestjs/swagger';
import { CreatePlaceDateTradDto } from './create-place-date.dto';

export class UpdatePlaceDto extends PartialType(CreatePlaceDateTradDto) {}
export class UpdatePlaceReqDto extends PartialType(CreatePlaceDateTradDto) {}
+30 −2
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import {
  Query,
  UnauthorizedException,
  Req,
  Patch,
} from '@nestjs/common';
import { PlaceService } from './place.service';
import { CreatePlaceDateTradDto } from './dto/create-place-date.dto';
@@ -19,6 +20,7 @@ import { fileInterceptor } from 'src/shared/interceptors/file-save.interceptor';
import { FileValidationPipe } from 'src/shared/pipe/file-validation.pipe';
import { LANGUAGES } from 'src/shared/enum/languages.enum';
import { CustomAdminRequest } from 'src/auth/admin/interface/customAdminReq';
import { UpdatePlaceReqDto } from './dto/update-place.req.dto';

@Controller('place')
@ApiTags('Place')
@@ -59,7 +61,33 @@ export class PlaceController {
  }

  @Get(':idPlace')
  findOne(@Param('idPlace') idPlace: number) {
    return this.placeService.findOneAndTradAndAvailable(idPlace);
  @ApiQuery({ name: 'lang', type: String })
  findOne(@Param('idPlace') idPlace: number, @Query('lang') lang: string) {
    return this.placeService.findOneAndTradAndAvailable(idPlace, lang as LANGUAGES);
  }

  @Patch(':idPlace')
  @Roles([ALL_ROLES.ADMIN])
  @ApiBearerAuth('jwt')
  @ApiConsumes('multipart/form-data')
  @ApiBody({ type: CreatePlaceDateTradDto })
  @UseInterceptors(fileInterceptor('image', 'static/places/', ['.jpg', '.jpeg', '.png']))
  async update(
    @UploadedFile(new FileValidationPipe()) file,
    @Body() updatePlaceReqDto: UpdatePlaceReqDto,
    @Param('idPlace') idPlace: number,
    @Req() req: CustomAdminRequest,
  ) {
    try {
      const place = await this.placeService.findOne(idPlace);
      console.log({ place });
      if (req.admin.idTown.townId != updatePlaceReqDto.idTown) {
        throw new UnauthorizedException('This is not your assigned town');
      }
      updatePlaceReqDto.image = file;
      return await this.placeService.update(idPlace, updatePlaceReqDto);
    } catch (e) {
      throw e;
    }
  }
}
+39 −1
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import { Town } from 'src/town/entities/town.entity';
import { GetPlaceDto } from './dto/get-place.dto';
import { ServerConstants } from 'src/constants/server.contants';
import { Available } from 'src/pointOfInterest/enum/available.enum';
import { UpdatePlaceReqDto } from './dto/update-place.req.dto';

@Injectable()
export class PlaceService {
@@ -112,7 +113,7 @@ export class PlaceService {
    return await this.placeRepository.findOneBy({ idPlace: id });
  }

  async findOneAndTradAndAvailable(idPlace: number) {
  async findOneAndTradAndAvailable(idPlace: number, lang: LANGUAGES) {
    const place: GetPlaceDto = await this.dataSource
      .getRepository(PlaceTraduction)
      .createQueryBuilder('placeTrad')
@@ -134,6 +135,7 @@ export class PlaceService {
        'place.available AS available',
      ])
      .where('place.idPlace = :idPlace', { idPlace: idPlace })
      .andWhere('placeTrad.language = :language', { language: lang })
      .getRawOne();

    return {
@@ -151,4 +153,40 @@ export class PlaceService {
      endDate: place.endDate,
    };
  }

  async update(idPlace: number, updatePlaceReqDto: UpdatePlaceReqDto) {
    const place: Place = await this.placeRepository.findOneBy({ idPlace });
    const createPlaceDto: CreatePlaceDto = {
      available: updatePlaceReqDto.available,
      closeAt: updatePlaceReqDto.closeAt,
      latitude: updatePlaceReqDto.latitude,
      longitude: updatePlaceReqDto.longitude,
      name: updatePlaceReqDto.name,
      openAt: updatePlaceReqDto.openAt,
      imageName: updatePlaceReqDto.image.filename,
      idTown: place.idTown,
    };

    await this.placeRepository.update({ idPlace }, createPlaceDto);
    await this.placeTraductionRepository
      .createQueryBuilder()
      .update(PlaceTraduction)
      .set({ description: updatePlaceReqDto.descriptionEN })
      .where('idPlace = :idPlace AND language = :language', { idPlace, language: LANGUAGES.EN })
      .execute();

    await this.placeTraductionRepository
      .createQueryBuilder()
      .update(PlaceTraduction)
      .set({ description: updatePlaceReqDto.descriptionES })
      .where('idPlace = :idPlace AND language = :language', { idPlace, language: LANGUAGES.ES })
      .execute();

    if (updatePlaceReqDto.available === Available.CUSTOM) {
      await this.availableDateRepository.update(
        { idPlace: place },
        { startDate: updatePlaceReqDto.startDate, endDate: updatePlaceReqDto.endDate },
      );
    }
  }
}