Blame view

src/api/experience/controllers/experience.js 4.69 KB
jaymehta committed
1 2 3
"use strict";
const utils = require("@strapi/utils");
const { ApplicationError, ValidationError } = utils.errors;
jay committed
4 5 6 7
/**
 * experience controller
 */

jaymehta committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21
const { createCoreController } = require("@strapi/strapi").factories;

module.exports = createCoreController("api::experience.experience", () => ({
  async create(ctx) {
    // Validations
    if (!ctx.request.body.data.vendor) {
      throw new ValidationError("Vendor is a mandatory field.");
    }
    if (!ctx.request.body.data.pincode) {
      throw new ValidationError("Pincode is a mandatory field.");
    }
    if (!ctx.request.body.data.subCategory) {
      throw new ValidationError("Subcategory is a mandatory field");
    }
jaymehta committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    // const months = ctx.request.body.data.months;
    // if (!months || !months.length > 0) {
    //   throw new ValidationError("Months is a required field.");
    // }
    // let monthsIds = [];
    // for (let index = 0; index < months.length; index++) {
    //   const element = months[index];
    //   const singleMonth = await strapi
    //     .query("api::master-month.master-month")
    //     .findOne({
    //       where: {
    //         name: element.value,
    //       },
    //     });
    //   // console.log("singleMonth", singleMonth);
    //   monthsIds = [...monthsIds, singleMonth.id];
    // }
jaymehta committed
39

jaymehta committed
40 41 42 43 44 45 46 47 48
    const subCategory = await strapi
      .query("api::sub-categorie.sub-categorie")
      .findOne({
        where: {
          name: {
            $eq: ctx.request.body.data.subCategory,
          },
        },
      });
jaymehta committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

    const repeatedActivities = await strapi
      .query("api::experience.experience")
      .findOne({
        where: {
          $and: [
            { name: ctx.request.body.data.name },
            { vendor: ctx.request.body.data.vendor.id },
          ],
        },
      });

    if (repeatedActivities) {
      throw new ValidationError(
        "Activity with the same name already exists, please rename the activity."
      );
    }
    // Create Activity
    const activity = await strapi.entityService.create(
      "api::experience.experience",
      {
        data: {
          name: ctx.request.body.data.name,
          description: ctx.request.body.data.description,
          pricePerPerson: ctx.request.body.data.pricePerPerson,
          address: ctx.request.body.data.address,
          masterPincode: ctx.request.body.data.pincode.id,
          minimumDuration: ctx.request.body.data.minimumDuration,
          maximumDuration: ctx.request.body.data.maximumDuration,
.  
jaymehta committed
78
          duration: ctx.request.body.data.duration,
jaymehta committed
79 80 81 82 83 84 85 86 87 88
          ageLowerLimit: ctx.request.body.data.ageLowerLimit,
          ageNotes: ctx.request.body.data.ageNotes,
          phoneNumber: ctx.request.body.data.phoneNumber,
          minGroupSize: ctx.request.body.data.minGroupSize,
          maxGroupSize: ctx.request.body.data.maxGroupSize,
          activityType: ctx.request.body.data.activityType,
          link: ctx.request.body.data.link,
          cancellationPolicy: ctx.request.body.data.cancellationPolicy,
          vendor: ctx.request.body.data.vendor.id,
          subCategory: subCategory.id,
jaymehta committed
89 90 91 92 93 94 95 96 97 98 99
          // masterMonths: monthsIds,
          giftSomeone: ctx.request.body.data.giftSomeone,
          fromDate: ctx.request.body.data.fromDate,
          toDate: ctx.request.body.data.toDate,
          fromTime: ctx.request.body.data.fromTime,
          toTime: ctx.request.body.data.toTime,
          offers: ctx.request.body.data.offers,
          rating: ctx.request.body.data.rating,
          contactPersonForActivity: ctx.request.body.data.contactPersonForActivity,
          contactPersonForBooking: ctx.request.body.data.contactPersonForBooking,
          ...ctx.request.body.data.daysBoolean,
jaymehta committed
100 101 102 103 104 105
        },
      }
    );

    // [{day: "monday", fromTime: "10:20:00", toTime: "18:30:00"}, {day: "tuesday", fromTime: "09:30:00", toTime: "20:00:00"}]

jaymehta committed
106 107 108 109 110 111 112
    // const timeSlotsArray = ctx.request.body.data.timeSlots;
    // if (!timeSlotsArray || !timeSlotsArray.length > 0) {
    //   throw new ValidationError("No time slots given.");
    // }
    // let timeSlotIds = [];
    // for (let index = 0; index < timeSlotsArray.length; index++) {
    //   const element = timeSlotsArray[index];
jaymehta committed
113

jaymehta committed
114 115 116 117 118 119 120 121 122 123 124 125 126
    //   const timeSlotEntry = await strapi.entityService.create(
    //     "api::time-slot.time-slot",
    //     {
    //       data: {
    //         masterDay: element.day.id,
    //         start: element.fromTime,
    //         end: element.toTime,
    //         experience: activity.id,
    //       },
    //     }
    //   );
    //   timeSlotIds = [...timeSlotIds, timeSlotEntry];
    // }
jay committed
127

jaymehta committed
128 129 130
    ctx.send({
      success: true,
      message: "Activity Created",
jaymehta committed
131 132 133 134
      data: {
        ...activity,
        // timeSlots: { ...timeSlotIds }
      },
jaymehta committed
135 136 137
    });
  },
}));