import { Body, Controller, Delete, Get, HttpException, HttpStatus, Param, Post, Put, Request, UseGuards, } from '@nestjs/common'; import { ApiBody, ApiParam, ApiTags } from '@nestjs/swagger'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { StructuresService } from '../structures/services/structures.service'; import { IsPersonalOfferOwnerGuard } from '../users/guards/isPersonalOfferOwner.guard'; import { IUser } from '../users/interfaces/user.interface'; import { UsersService } from '../users/services/users.service'; import { CreatePersonalOfferDto } from './dto/create-personal-offer.dto'; import { PersonalOfferDto } from './dto/personal-offer.dto'; import { PersonalOffersService } from './personal-offers.service'; import { PersonalOffer, PersonalOfferDocument } from './schemas/personal-offer.schema'; import { Types } from 'mongoose'; @ApiTags('personnal-offers') @Controller('personal-offers') export class PersonalOffersController { constructor( private personalOffersService: PersonalOffersService, private structuresService: StructuresService, private usersService: UsersService ) {} /** * Return personal offer of given id. */ @Get(':id') @ApiParam({ name: 'id', type: String, required: true }) public async find(@Param('id') id: string): Promise<PersonalOffer> { return this.personalOffersService.findOne(id); } /** * Create and return a personal offer. */ @Post() @ApiBody({ type: CreatePersonalOfferDto, required: true }) @UseGuards(JwtAuthGuard) public async create( @Request() req, @Body() createPersonalOfferDto: CreatePersonalOfferDto ): Promise<PersonalOfferDocument> { const user: IUser = req.user; const isEmpty = Object.values(createPersonalOfferDto.personalOffer.categories).every((value) => value.length === 0); if (isEmpty) { throw new HttpException('Empty personal offer', HttpStatus.NO_CONTENT); } const personalOfferDocument: PersonalOfferDocument = await this.personalOffersService.create( createPersonalOfferDto ); await this.structuresService.addPersonalOffer(createPersonalOfferDto.structureId, personalOfferDocument); await this.usersService.addPersonalOffer(user._id, personalOfferDocument); // Update if structure has user with personalOffer (hasUserWithAppointmentDN) this.structuresService.updateDenormalizedFields(new Types.ObjectId(createPersonalOfferDto.structureId)); return personalOfferDocument; } /** * Update and return a personal offer. */ @Put(':id') @ApiParam({ name: 'id', type: String, required: true }) @ApiBody({ type: PersonalOfferDto, required: true }) @UseGuards(JwtAuthGuard, IsPersonalOfferOwnerGuard) public async update( @Param('id') id: string, @Body() updatePersonalOfferDto: PersonalOfferDto ): Promise<PersonalOfferDocument> { return this.personalOffersService.update(id, updatePersonalOfferDto); } /** * Delete and return a personal offer. */ @Delete(':id') @ApiParam({ name: 'id', type: String, required: true }) @UseGuards(JwtAuthGuard, IsPersonalOfferOwnerGuard) public async delete(@Param('id') id: string): Promise<PersonalOfferDocument> { return this.personalOffersService.delete(id); } }