Untitled

mail@pastecode.io avatar
unknown
typescript
3 years ago
1.2 kB
3
Indexable
public validateDates(
    updateCampaignDto: UpdateCampaignDto,
    campaign: any, //CampaignEntity
  ) {
    const startDate = Date.parse(
      updateCampaignDto.startDate || campaign.startDate,
    );
    const endDate = Date.parse(updateCampaignDto.endDate || campaign.endDate);
    if (startDate >= endDate) {
      throw new BadRequestException(
        `Start date should be earlier than end date`,
      );
    }

    const artworkDate = updateCampaignDto.artworkDate || campaign.artworkDate;
    if (artworkDate && startDate <= Date.parse(artworkDate)) {
      throw new BadRequestException(
        `Artwork date should be earlier than start date`,
      );
    }

    const dispatchDate =
      updateCampaignDto.dispatchDate || campaign.dispatchDate;
    if (dispatchDate && startDate <= Date.parse(dispatchDate)) {
      throw new BadRequestException(
        `Dispatch date should be earlier than start date`,
      );
    }

    if (dispatchDate && artworkDate && dispatchDate < artworkDate) {
      throw new BadRequestException(
        `Artwork date should be earlier than dispatch date`
      );
    }
  }