Blame view

src/api/experience/controllers/experience.js 11.4 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
    if (!ctx.request.body.data.category) {
      throw new ValidationError("Category is a mandatory field");
    }
jaymehta committed
25 26 27 28 29 30 31 32 33
    const subCategory = await strapi
      .query("api::sub-categorie.sub-categorie")
      .findOne({
        where: {
          name: {
            $eq: ctx.request.body.data.subCategory,
          },
        },
      });
jaymehta committed
34

jaymehta committed
35 36 37 38
    const category = await strapi.query("api::categorie.categorie").findOne({
      where: {
        name: {
          $eq: ctx.request.body.data.category,
.  
jaymehta committed
39
        },
jaymehta committed
40 41
      },
    });
.  
jaymehta committed
42

jaymehta committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    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."
      );
    }
jaymehta committed
59
    let finalImageArray = [];
.  
jaymehta committed
60

jaymehta committed
61 62 63 64 65 66
    for (
      let index = 0;
      index < ctx.request.body.data.imagesComponent.length;
      index++
    ) {
      const element = ctx.request.body.data.imagesComponent[index];
.  
jaymehta committed
67
      console.log("here >", element);
jaymehta committed
68
      finalImageArray = [...finalImageArray, { image: element }];
.  
jaymehta committed
69
      console.log("final array", finalImageArray);
jaymehta committed
70
    }
.  
jaymehta committed
71

jaymehta committed
72
    // ctx.request.body.data.imagesComponent;
jaymehta committed
73 74 75 76 77 78 79 80 81 82 83 84
    // 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
85
          duration: ctx.request.body.data.duration,
jaymehta committed
86 87 88 89 90 91 92 93 94 95
          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
96
          category: category.id,
jaymehta committed
97 98 99 100 101 102 103
          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,
jaymehta committed
104 105 106 107
          contactPersonForActivity:
            ctx.request.body.data.contactPersonForActivity,
          contactPersonForBooking:
            ctx.request.body.data.contactPersonForBooking,
.  
jaymehta committed
108
          approved: ctx.request.body.data.approved,
.  
jaymehta committed
109
          imagesComponent: finalImageArray,
.  
jaymehta committed
110
          // imagesComponent: finalImageArray,
jaymehta committed
111
          ...ctx.request.body.data.daysBoolean,
jaymehta committed
112 113 114
          image: ctx.request.body.data.image,
          termsConditions: ctx.request.body.data.termsConditions,

jaymehta committed
115 116 117 118
        },
      }
    );

jaymehta committed
119 120 121 122 123 124 125 126
    ctx.send({
      success: true,
      message: "Activity Created",
      data: {
        ...activity,
      },
    });
  },
jaymehta committed
127

.  
jaymehta committed
128
  async update(ctx) {
.  
jaymehta committed
129 130
    const { id } = ctx.params;

.  
jaymehta committed
131 132 133
    // if (!ctx.request.body.data.vendor) {
    //   throw new ValidationError("Vendor is a mandatory field.");
    // }
.  
jaymehta committed
134 135 136 137 138 139 140 141 142 143
    if (
      ctx.request.body.data.approved == "approved" ||
      ctx.request.body.data.approved == "rejected"
    ) {
      const updatedExperience = await strapi
        .query("api::experience.experience")
        .update({
          where: { id },
          data: {
            approved: ctx.request.body.data.approved,
.  
jaymehta committed
144
            rejectionReason: ctx.request.body.data.rejectionReason,
.  
jaymehta committed
145 146 147
          },
        });
      console.log("updatedExperience", updatedExperience);
.  
jaymehta committed
148
      return ctx.send({
.  
jaymehta committed
149 150 151 152 153 154 155
        success: true,
        message: "Status updated",
        data: {
          ...updatedExperience,
        },
      });
    }
.  
jaymehta committed
156
    console.log("ctx>", ctx.request.body);
.  
jaymehta committed
157
    if (!ctx.request.body.data.pincode) {
.  
jaymehta committed
158 159
      throw new ValidationError("Pincode is a mandatory field.");
    }
.  
jaymehta committed
160
    if (!ctx.request.body.data.subCategory) {
.  
jaymehta committed
161 162
      throw new ValidationError("Subcategory is a mandatory field");
    }
.  
jaymehta committed
163
    if (!ctx.request.body.data.category) {
.  
jaymehta committed
164 165 166
      throw new ValidationError("Category is a mandatory field");
    }
    console.log("subcategoyr", ctx.params);
.  
jaymehta committed
167 168 169 170 171
    console.log(
      "ctx.request.body.data.imagesComponent",
      ctx.request.body.data.imagesComponent,
      ctx.request.body.data.imagesComponent.length
    );
jaymehta committed
172 173 174 175 176 177 178
    let finalImageArray = [];
    for (
      let index = 0;
      index < ctx.request.body.data.imagesComponent.length;
      index++
    ) {
      const element = ctx.request.body.data.imagesComponent[index];
.  
jaymehta committed
179
      console.log("element", element);
.  
jaymehta committed
180
      finalImageArray = [...finalImageArray, { image: element }];
.  
jaymehta committed
181
      console.log("final", finalImageArray);
jaymehta committed
182
    }
.  
jaymehta committed
183 184 185 186 187
    const subCategory = await strapi
      .query("api::sub-categorie.sub-categorie")
      .findOne({
        where: {
          name: {
.  
jaymehta committed
188
            $eq: ctx.request.body.data.subCategory,
.  
jaymehta committed
189 190 191 192 193 194
          },
        },
      });
    const category = await strapi.query("api::categorie.categorie").findOne({
      where: {
        name: {
.  
jaymehta committed
195
          $eq: ctx.request.body.data.category,
.  
jaymehta committed
196 197 198 199
        },
      },
    });
    console.log(
.  
jaymehta committed
200 201
      "ctx.request.body.data.activityId",
      ctx.request.body.data.activityId
.  
jaymehta committed
202
    );
.  
jaymehta committed
203

.  
jaymehta committed
204 205 206 207
    const updatedExperience = await strapi.entityService.update(
      "api::experience.experience",
      id,
      {
.  
jaymehta committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
        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,
          duration: ctx.request.body.data.duration,
          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,
          subCategory: subCategory.id,
          category: category.id,
          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,
.  
jaymehta committed
238
          imagesComponent: finalImageArray,
.  
jaymehta committed
239
          approved: ctx.request.body.data.approved,
.  
jaymehta committed
240
          ...ctx.request.body.data.daysBoolean,
.  
jaymehta committed
241
          image: ctx.request.body.data.image,
jaymehta committed
242
          termsConditions: ctx.request.body.data.termsConditions,
.  
jaymehta committed
243
        },
.  
jaymehta committed
244 245 246
      }
    );

.  
jaymehta committed
247 248 249 250 251 252 253 254 255 256
    console.log(
      "ctx.request.body.data.imagesArrayComponent",
      ctx.request.body.data.imagesComponent
    );
    // const imagesComponentData = Array.isArray(
    //   ctx.request.body.data.imagesArrayComponent
    // )
    //   ? ctx.request.body.data.imagesArrayComponent
    //   : [ctx.request.body.data.imagesArrayComponent];
    // console.log("imagesComponentData >", imagesComponentData);
jaymehta committed
257 258 259 260 261 262 263
    // const updatedExperienceImageComponent = await strapi.entityService.update(
    //   "api::experience.experience",
    //   id,
    //   {
    //     data: { imagesComponent: ctx.request.body.data.imagesComponent },
    //   }
    // );
.  
jaymehta committed
264 265 266 267 268 269 270 271 272
    console.log("updatedExperience", updatedExperience);
    ctx.send({
      success: true,
      message: "Activity updated",
      data: {
        ...updatedExperience,
      },
    });
  },
jaymehta committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

  async updateImage(ctx) {
    console.log(ctx.request.body);

    const id = ctx.request.body.id; // Get the ID of the experience entry
    if (!id) {
      return ctx.notFound("Please mention experience ID");
    }
    if (!ctx.request.body.newComponentData > 0) {
      return ctx.notFound("Please mention the URLs");
    }
    const newComponentData = ctx.request.body.newComponentData; // Get the new data for the repeatable component
    try {
      const existingExperience = await strapi.entityService.findOne(
        "api::experience.experience",
        id
      );

      if (!existingExperience) {
        return ctx.notFound("Experience with the given ID not found.");
      }

      // Clear the existing repeatable component data
      await strapi.entityService.update("api::experience.experience", id, {
        data: { imagesComponent: [] },
      });
      // Update the entry with new data
      const updatedExperience = await strapi.entityService.update(
        "api::experience.experience",
        id,
        {
          data: { imagesComponent: newComponentData },
        }
      );

      return ctx.send(updatedExperience);
    } catch (error) {
      ctx.throw(500, error);
    }
  },
jaymehta committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

  async getSearchResponse(ctx) {
    const string = ctx.request.body.string;
    console.log("str", ctx.request.body.string);
    if (!string.length > 0) {
      return ctx.notFound("Please pass a valid string");
    }
    const activities = await strapi.db
      .query("api::experience.experience")
      .findMany({
        where: {
          name: {
            $contains: string,
          },
        },
      });
    const categories = await strapi.db
      .query("api::categorie.categorie")
      .findMany({
        where: {
          name: {
            $contains: string,
          },
        },
      });
    console.log("activities", activities);
    console.log("categories", categories);
    let responseData = {};
.  
jaymehta committed
341 342 343
    responseData["categories"] = categories?.map((item) => {
      return { id: item.id, name: item.name };
    });
.  
jaymehta committed
344

jaymehta committed
345 346 347 348 349 350 351 352 353
    responseData["activities"] = activities?.map((item) => {
      return { id: item.id, name: item.name };
    });

    return ctx.send({
      success: true,
      data: responseData,
    });
  },
jaymehta committed
354
}));