|
1 | | -import { BadRequestException, forwardRef, Inject, Injectable, Logger, NotFoundException } from '@nestjs/common'; |
| 1 | +import { forwardRef, Inject, Injectable, Logger, NotFoundException } from '@nestjs/common'; |
2 | 2 | import { InjectModel } from '@nestjs/mongoose'; |
3 | 3 | import { Model, PipelineStage, Types } from 'mongoose'; |
4 | 4 | import { GardenService } from '../garden/garden.service'; |
5 | | -import { GrowingZoneData, PLANTED, STARTED_FROM_TYPE_SEED, TaskType } from '../interface'; |
| 5 | +import { GrowingZoneData, STARTED_FROM_TYPE_SEED, TaskType } from '../interface'; |
6 | 6 | import { PlantInstanceService } from '../plant-instance/plant-instance.service'; |
7 | | -import { PlantInstanceDTO } from '../plant-instance/dto/plant-instance.dto'; |
8 | 7 | import { TaskProjection } from '../task/interfaces/task.projection'; |
9 | 8 | import { TaskService } from '../task/services/task.service'; |
10 | 9 | import { UserService } from '../users/user.service'; |
11 | 10 | import { fromTaskTypeToHistoryStatus } from '../util/history.util'; |
12 | 11 | import { isNotNullish } from '../util/null.util'; |
13 | 12 | import computeSeason from '../util/season.util'; |
14 | | -import { ContainerPlanSlotDTO } from './dto/container-plan-slot.dto'; |
15 | 13 | import { ContainerSlotDTO } from './dto/container-slot.dto'; |
16 | 14 | import { ContainerDTO, sanitizeContainerDTO } from './dto/container.dto'; |
17 | 15 | import { Slot } from './interfaces/container-slot.interface'; |
@@ -288,7 +286,11 @@ export class ContainerService { |
288 | 286 | plantId: string | undefined, |
289 | 287 | growingZoneData: GrowingZoneData |
290 | 288 | ) { |
291 | | - const plantInstance = await this.plantInstanceService.getPlantInstance(slot.plantInstanceId, userId, gardenId); |
| 289 | + const plantInstance = await this.plantInstanceService.getPlantInstance( |
| 290 | + slot != null ? slot.plantInstanceId : null, |
| 291 | + userId, |
| 292 | + gardenId |
| 293 | + ); |
292 | 294 | if (plantInstance && (!plantId || plantInstance.plant === plantId)) { |
293 | 295 | this.plantInstanceService.createUpdateTasks(userId, gardenId, container, plantInstance, path, growingZoneData); |
294 | 296 | } |
@@ -371,94 +373,4 @@ export class ContainerService { |
371 | 373 |
|
372 | 374 | return plantInstancesCreatedCount; |
373 | 375 | } |
374 | | - |
375 | | - async planContainerSlot(containerId: string, userId: string, gardenId: string, dto: ContainerPlanSlotDTO) { |
376 | | - const slotId = Number(dto.slotId); |
377 | | - if (!Number.isInteger(slotId) || slotId < 0) { |
378 | | - throw new BadRequestException('Invalid slotId'); |
379 | | - } |
380 | | - |
381 | | - const plantId = `${dto.plantId ?? ''}`.trim(); |
382 | | - if (!plantId) { |
383 | | - throw new BadRequestException('Invalid plantId'); |
384 | | - } |
385 | | - |
386 | | - const container = await this.getContainer(containerId, userId, gardenId); |
387 | | - if (!container) { |
388 | | - throw new NotFoundException('Container does not exist!'); |
389 | | - } |
390 | | - |
391 | | - const maxSlots = (container.rows ?? 0) * (container.columns ?? 0); |
392 | | - if (slotId >= maxSlots) { |
393 | | - throw new BadRequestException('slotId is out of range'); |
394 | | - } |
395 | | - |
396 | | - const slot = container.slots?.[`${slotId}`]; |
397 | | - |
398 | | - if (!slot?.plantInstanceId) { |
399 | | - return this.plantInstanceService.addPlantInstance( |
400 | | - { |
401 | | - containerId, |
402 | | - slotId, |
403 | | - plant: plantId, |
404 | | - created: new Date().toISOString(), |
405 | | - startedFrom: container.startedFrom ?? STARTED_FROM_TYPE_SEED, |
406 | | - season: computeSeason() |
407 | | - }, |
408 | | - userId, |
409 | | - gardenId |
410 | | - ); |
411 | | - } |
412 | | - |
413 | | - const plantInstance = await this.plantInstanceService.getPlantInstance(slot.plantInstanceId, userId, gardenId); |
414 | | - if (!plantInstance || !plantInstance._id) { |
415 | | - throw new NotFoundException('Plant instance does not exist!'); |
416 | | - } |
417 | | - |
418 | | - const hasPlantedEvent = (plantInstance.history ?? []).some((history) => history.status === PLANTED); |
419 | | - const isPlanning = !plantInstance.closed && !hasPlantedEvent; |
420 | | - |
421 | | - if (plantInstance.closed) { |
422 | | - return this.plantInstanceService.addPlantInstance( |
423 | | - { |
424 | | - containerId, |
425 | | - slotId, |
426 | | - plant: plantId, |
427 | | - created: new Date().toISOString(), |
428 | | - startedFrom: container.startedFrom ?? STARTED_FROM_TYPE_SEED, |
429 | | - season: computeSeason() |
430 | | - }, |
431 | | - userId, |
432 | | - gardenId |
433 | | - ); |
434 | | - } |
435 | | - |
436 | | - if (!isPlanning) { |
437 | | - throw new BadRequestException('Only closed or planning slots can be updated'); |
438 | | - } |
439 | | - |
440 | | - const updateDto: PlantInstanceDTO = { |
441 | | - containerId, |
442 | | - slotId, |
443 | | - plant: plantId, |
444 | | - created: new Date(plantInstance.created).toISOString(), |
445 | | - comments: plantInstance.comments?.map((comment) => ({ |
446 | | - ...comment, |
447 | | - date: new Date(comment.date).toISOString() |
448 | | - })), |
449 | | - pictures: plantInstance.pictures?.map((picture) => ({ |
450 | | - ...picture, |
451 | | - date: new Date(picture.date).toISOString() |
452 | | - })), |
453 | | - history: plantInstance.history?.map((history) => ({ |
454 | | - ...history, |
455 | | - date: new Date(history.date).toISOString() |
456 | | - })), |
457 | | - closed: plantInstance.closed, |
458 | | - startedFrom: plantInstance.startedFrom, |
459 | | - season: plantInstance.season |
460 | | - }; |
461 | | - |
462 | | - return this.plantInstanceService.editPlantInstance(plantInstance._id, userId, gardenId, updateDto); |
463 | | - } |
464 | 376 | } |
0 commit comments