GIF89a php
Current File : /home/viralhoga/app_viralhoga/src/modules/programBooking/programBooking.controller.js
import { ArtistBookingModel } from "../../models/artistBooking.model.js";
import { BookingArtistModel } from "../../models/programBooking.model.js";
import User from "../../models/user.model.js";
import ApiError from "../../utils/ApiErrorHandler.js";
import ApiResponse from '../../utils/ApiRespinseHandler.js'
import AsyncHandler from "../../utils/AsyncHandler.js";
import path from 'path'

export const createBookingArtist = AsyncHandler(async (req, res) => {
    const { name, role, type } = req.body;

    if (!name || !role || !type) {
        throw new ApiError(400, 'All fields are required')
    }

    let profileImages = ''
    if (req.file?.path) {
        profileImages = `uploads/${path.basename(req.file.path)}`
    }

    const bookingArtist = await BookingArtistModel.create({
        name,
        role,
        type,
        profileImages
    })

    return res.status(201).json(
        new ApiResponse(201, 'Artist created successfully', bookingArtist)
    )

})

export const getBookingArtists = AsyncHandler(async (req, res) => {
    const queryObj = {}
    if (req.query.userType) {
        queryObj.type = req.query.userType
    }

    console.log('queryObj', queryObj)
    const artists = await BookingArtistModel.find(queryObj);

    return res.status(200).json(
        new ApiResponse(
            200,
            "Artists fetched successfully",
            artists
        )
    );
});

export const updateBookingArtist = AsyncHandler(async (req, res) => {
    const { id } = req.params;
    const { name, role, type } = req.body;

    const artist = await BookingArtistModel.findById(id);

    if (!artist) {
        throw new ApiError(404, "Artist not found");
    }

    if (name) artist.name = name;
    if (role) artist.role = role;
    if (type) artist.type = type;

    if (req.file?.path) {
        artist.profileImages = `uploads/${path.basename(req.file.path)}`;
    }

    await artist.save();

    return res.status(200).json(
        new ApiResponse(
            200,
            "Artist updated successfully",
            artist
        )
    );
});

export const deleteBookingArtist = AsyncHandler(async (req, res) => {
    const { id } = req.params;

    const artist = await BookingArtistModel.findById(id);

    if (!artist) {
        throw new ApiError(404, "Artist not found");
    }

    await BookingArtistModel.findByIdAndDelete(id);

    return res.status(200).json(
        new ApiResponse(
            200,
            "Artist deleted successfully",
            null
        )
    );
});

/**
 * ARTIST BOOKING APIs FOR PROGRAM/
 */

export const bookArtist = AsyncHandler(async (req, res) => {

    console.log('req.body', req.body)
    const artistId = req.params.artistId;
    if (!artistId) throw new ApiError(400, 'artist id missing in params');
    
    const {
        organizerName,
        eventDate,
        city,
        state,
        country,
        contactNumber,
        emailId,
    } = req.body;

    if (
        !organizerName ||
        !eventDate ||
        !city ||
        !state ||
        !country ||
        !contactNumber
    ) {
        throw new ApiError(400, "All fields are required");
    }

    // const isValidArtistId = await User.findOne({ authId: artistId })
    const isValidArtistId = await BookingArtistModel.findById(artistId )
    if (!isValidArtistId) throw new ApiError(400, 'Invalid artist id');

    const booking = await ArtistBookingModel.create({
        artistId,
        organizerName,
        eventDate,
        city,
        state,
        country,
        contactNumber,
        emailId,
    });

    return res.status(201).json(
        new ApiResponse(
            201,
            "Your Booking request has been sent successfully, will connect you soon... ",
            booking
        )
    );
});

export const getBookedArtist = AsyncHandler(async (req, res) => {
    const bookings = await ArtistBookingModel.find()
        .sort({ createdAt: -1 });

    return res.status(200).json(
        new ApiResponse(
            200,
            "Bookings fetched successfully",
            bookings
        )
    );
});