NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

const { Appointment, freeTimeSlot } = require("../model/appointment.model");
const profile = require("../../user/model/profile.model");
const responseMessage = require("../../../middleware/responseMessage");
const statusCode = require("../../../middleware/statusCode");
const mongoose = require("mongoose")
var datejs = require('safe_datejs');

const setResponseObject =
require("../../../middleware/commonFunction").setResponseObject;

const moment = require('moment');

const _appointment = {};

/**
* Booking expert user time
*/
_appointment.bookAppointment = async (req, res, next) => {
try {
let data = req.body;
let isAppointmentBooked = await new Appointment(data).save();
if (!isAppointmentBooked) {
res
.send(statusCode.BAD_REQUEST)
.send({
success: false,
message: responseMessage.SOMETHING_WRONG("Failed to book"),
});
} else {
res
.send(statusCode.SUCCESS)
.send({
success: true,
message: responseMessage.ADD_SUCCESS("Appointment"),
data: isAppointmentBooked,
});
}
} catch (err) {
await setResponseObject(req, false, err.message, "");
next();
}
};

/**
* Get my bookings by user
*/
_appointment.getMyBookings = async (req, res, next) => {
try {
let isResult = await Appointment.find().populate('bookUser').populate('bookedBy');
if (!isResult) {
res.send(statusCode.BAD_REQUEST)
.send({
success: false,
message: responseMessage.NOT_FOUND("Appointment"),
});
} else
{
res.send(statusCode.SUCCESS)
.send({
success: true,
message: responseMessage.FOUND_SUCCESS("Appointment"),
data: isResult,
});
}
} catch (err) {
await setResponseObject(req, false, err.message, "");
next();
}
};

/**
* Cancel appointments by user
*/
_appointment.cancel = async(req,res,next) => {
try {
let isResult = await Appointment.findOneAndUpdate(
{_id: req.params.appointmentId},
{status: "Cancelled"},
{new: true}
);
if(isResult){
res.status(statusCode.SUCCESS)
.send({
success: true,
message: responseMessage.GENERAL_MESSAGE("Appointment cancel successfully"),
data: isResult
})
} else
{
res.send(statusCode.BAD_REQUEST)
.send({
success: false,
message: responseMessage.SOMETHING_WRONG("Appointment"),
});
}
} catch (err) {
await setResponseObject(req, false, err.message, "");
next();
}
}

/**
* Reject Appointment by user
*/
_appointment.reject = async(req,res,next) => {
try {
let isResult = await Appointment.findOneAndUpdate(
{_id: req.params.appointmentId},
{status: "Rejected"},
{new: true}
);
if(isResult){
res.status(statusCode.SUCCESS)
.send({
success: true,
message: responseMessage.GENERAL_MESSAGE("Appointment reject successfully"),
data: isResult
})
} else
{
res.send(statusCode.BAD_REQUEST)
.send({
success : false,
message : responseMessage.SOMETHING_WRONG("Appoinment"),
});
}
}
catch (err) {
await setResponseObject(req, false, err.message, "");
next();
}
}

/**
* Delete Appointment
*/
_appointment.delete = async(req,res,next) => {
try {
let isResult = await Appointment.findOneAndUpdate(
{_id: req.params.appointmentId},
{status: "Rejected"},
{new: true}
);
if(isResult){
res.status(statusCode.SUCCESS)
.send({
success: true,
message: responseMessage.GENERAL_MESSAGE("Appointment cancel successfully"),
data: isResult
})
}else{
res.status(statusCode.BAD_REQUEST)
.send({
success : false,
message : responseMessage.SOMETHING_WRONG("Appoinment"),
});
}
} catch (err) {
await setResponseObject(req, false, err.message, "");
next();
}
}

_appointment.delete = async (req, res, next) =>{
// try {
// let isResult = await Appointment.findOneAndRemove(
// {_id : req.params.appointmentId},
// )
// }
}
// }catch(err)
// {
// await setResponseObject(req, false, err.message, "");
// next();
// }
// }

/**
* Create free time slot by expert
*/
_appointment.insertFreeTimeSlot = async (req, res, next) => {
try {
let data = req.body;
let isAppointmentBooked = await new freeTimeSlot(data).save();
if (!isAppointmentBooked) {
res
.send(statusCode.BAD_REQUEST)
.send({
success: false,
message: responseMessage.SOMETHING_WRONG("Failed to book"),
});
} else {
res
.send(statusCode.SUCCESS)
.send({
success: true,
message: responseMessage.ADD_SUCCESS("Appointment"),
data: isAppointmentBooked,
});
}
} catch (err) {
await setResponseObject(req, false, err.message, "");
next();
}
};

/**
* update free time slot by expert
*/
_appointment.updateFreeTimeSlot = async (req, res, next) => {
try {
let data = req.body;
let isAppointmentBooked = await freeTimeSlot.findOneAndUpdate(
{_id: req.params.appointmentId },
data,
{new: true}
);
if (!isAppointmentBooked) {
res
.send(statusCode.BAD_REQUEST)
.send({
success: false,
message: responseMessage.SOMETHING_WRONG("Failed to book"),
});
} else {
res
.send(statusCode.SUCCESS)
.send({
success: true,
message: responseMessage.ADD_SUCCESS("Appointment"),
data: isAppointmentBooked,
});
}
} catch (err) {
await setResponseObject(req, false, err.message, "");
next();
}
};

_appointment.addFreeTimeSlot = async(req,res,next) => {
try{
let getdata = req.body;
req.body.userId = req.userId;
req.body.profileId = req.query.profileId;

// let getDifferentProfiles = await freeTimeSlot.find({userId : req.body.userId});
// // console.log(getDifferentProfiles,'getDifferentProfiles')

// let matchTimeSlots = await freeTimeSlot.aggregate([
// {
// $match : {$and : [{ $in: [ {timeSlot : req.body.timeSlot} ],userId : req.body.userId, Date : req.body.Date }]}
// }
// ])
// if(matchTimeSlots){
// res
// .send({
// success: false,
// message: responseMessage.TIME_SLOT_ALREADY_EXISTS,
// });
// }
// else{
let addFreeTime = await new freeTimeSlot(getdata).save();
if (!addFreeTime) {
res
.send({
success: false,
message: responseMessage.SOMETHING_WRONG("Failed to book"),
});
}
else {
res
.send({
success: true,
message: responseMessage.ADD_SUCCESS("Appointment"),
data: addFreeTime,
});
}
// }
}
catch (err) {
await setResponseObject(req, false, err.message, "");
next();
}
}

_appointment.getSlotByDate = async(req, res, next) => {
try{
let getSlotByDate = await freeTimeSlot.find({$and : [{profileId : req.query.profileId},{Date : req.query.Date}]})

// const changeDateFormat = moment(getSlotByDate.Date).format('YYYY-MM-DD');

console.log(changeDateFormat,'changeDateFormat')
console.log(getSlotByDate,'getSlotByDate********');
// console.log(profileId,'profileId^^^^^^^^^^^^^^^^');
// console.log(Date,'profileId+++++++++++++++');
// console.log(req.body,'-------------');
if(getSlotByDate){
console.log(getSlotByDate,"hello")
res
.send({
success: true,
message: responseMessage.RECORD_FOUND,
data: getSlotByDate,
});
}
else{
console.log('errr')
res
.send({
success: true,
message: responseMessage.RECORD_NOT_FOUND,
});
}
}
catch(err){
await setResponseObject(req, false, err.message, "");
next();
}
}

_appointment.getSlotsByDate = async(req, res, next) => {
try{
let mydate = req.query.Date;

let getSlotByDate = await freeTimeSlot.find({$and : [{profileId : req.query.profileId}]});
let lengthSlots = getSlotByDate.length;
let slotsArray = [];

var wrappedToday = mydate.AsDateJs();
console.log('Is today:', wrappedToday.is().today());

var tomorrow = wrappedToday.clone().add({days:1});
console.log('Wrapped Tomorrow:', tomorrow.toString(), 'is date regular Date: ', (tomorrow instanceof Date));

var unwrappedTomorrow = tomorrow.AsRegularDate();
console.log('Unwrapped Tomorrow:', unwrappedTomorrow.toString(), 'is date regular Date: ', (unwrappedTomorrow instanceof Date));

// let bodyDate = req.query.Date;
// let toISO = new Date(bodyDate).toISOString();
// console.log(toISO,"toISO")

let findDate = await freeTimeSlot.find({Date : req.query})

// for(let i = 0;i < lengthSlots;i++){
// let formatDate = moment(getSlotByDate[i].Date).format('YYYY-MM-DD');
// slotsArray.push(formatDate);
// }
// console.log(slotsArray,"slotsArrays")
// const changeDateFormat = moment(getSlotByDate.Date).format('YYYY-MM-DD');

// console.log(changeDateFormat,'changeDateFormat')
// console.log(getSlotByDate,'getSlotByDate********');
// console.log(profileId,'profileId^^^^^^^^^^^^^^^^');
// console.log(Date,'profileId+++++++++++++++');
// console.log(req.body,'-------------');
if(findDate){
console.log(findDate,"hello")
res
.send({
success: true,
message: responseMessage.RECORD_FOUND,
data: findDate,
});
}
else{
console.log('errr')
res
.send({
success: true,
message: responseMessage.RECORD_NOT_FOUND,
});
}
}
catch(err){
await setResponseObject(req, false, err.message, "");
next();
}
}

// _appointment.divisdeSlots = async(req, res, next) => {
// try{
// let getFromTime = req.body.from_Time;
// let getUptoTime = req.body.upto_Time;



// function(){

// }
// }
// }
// catch(err){
// await setResponseObject(req, false, err.message, "");
// next();
// }
// }

_appointment.checkFreeTimeSlot = async(req,res,next) => {
try{
let getdata = req.body;
let bodyDate = req.body.Date;
// req.body.userId = req.body.userId;
req.body.profileId = req.query.profileId;

let matchTimeSlots = await freeTimeSlot.find({Date : req.body.Date});
console.log(matchTimeSlots,"matchTimeSlots---------")

if(!matchTimeSlots ){
console.log("matchTimeSlots++++++++++")
let addFreeTime = await new freeTimeSlot(getdata).save();
if (!addFreeTime) {
res
.send({
success: false,
message: responseMessage.SOMETHING_WRONG("Failed to book"),
});
}
else {
console.log(addFreeTime,"matchTimeSlots++++++++++")
res
.send({
success: true,
message: responseMessage.ADD_SUCCESS("Appointment"),
data: addFreeTime,
});
}
}
else{
console.log("here________________++++++++++")
const changeDateFormat = moment(matchTimeSlots[0].Date).format('YYYY-MM-DD');
if(changeDateFormat != bodyDate){
let addFreeTime = await new freeTimeSlot(getdata).save();
if (!addFreeTime) {
res
.send({
success: false,
message: responseMessage.SOMETHING_WRONG("Failed to book"),
});
}
else {
res
.send({
success: true,
message: responseMessage.ADD_SUCCESS("Appointment"),
data: addFreeTime,
});
}
}
else{
res
.send({
success: false,
message: responseMessage.TIME_SLOT_ALREADY_EXISTS,
});
}
}
}
catch (err) {
await setResponseObject(req, false, err.message, "");
next();
}
}

_appointment.checFreeTimeSlot = async(req,res,next) => {
try{
let getdata = req.body;
let bodyDate = req.body.Date;
// req.body.userId = req.body.userId;
req.body.profileId = req.query.profileId;

let matchTimeSlots = await freeTimeSlot.findOne({Date : req.body.Date});

console.log(matchTimeSlots,"matchTimeSlots---------")

if(matchTimeSlots){

console.log("1 -------------")

const changeDateFormat = moment(matchTimeSlots.Date).format('YYYY-MM-DD');
console.log(changeDateFormat,"changeDateFormat+++++++++++")

console.log(bodyDate,"bodydate")

if (changeDateFormat != bodyDate ){

console.log("2 -----------------------")
console.log(changeDateFormat != bodyDate ,"answerrrrrrrrrrrrrrr")


let addFreeTime = await new freeTimeSlot(getdata).save();
if (!addFreeTime) {
res
.send({
success: false,
message: responseMessage.SOMETHING_WRONG("Failed to book"),
});
}
else {
res
.send({
success: true,
message: responseMessage.ADD_SUCCESS("Appointment"),
data: addFreeTime,
});
}
}
else if(changeDateFormat == bodyDate){

console.log("3 -------------------------------------")


res
.send({
success: false,
message: responseMessage.TIME_SLOT_ALREADY_EXISTS,
});
}

}
else{

console.log("4 ------------------------------------------------")

let addFreeTime = await new freeTimeSlot(getdata).save();
if (!addFreeTime) {
res
.send({
success: false,
message: responseMessage.SOMETHING_WRONG("Failed to book"),
});
}
else {
res
.send({
success: true,
message: responseMessage.ADD_SUCCESS("Appointment"),
data: addFreeTime,
});
}
}
}
catch (err) {
await setResponseObject(req, false, err.message, "");
next();
}
}

_appointment.bookFreeTimeSlot = async(req, res, next) => {
try{
let getdata = req.body;
let bookTimeSlot = await freeTimeSlot.findOneAndUpdate()
if (!bookTimeSlot) {
res
.send(statusCode.BAD_REQUEST)
.send({
success: false,
message: responseMessage.SOMETHING_WRONG("Failed to save"),
});
} else {
res
.send(statusCode.SUCCESS)
.send({
success: true,
message: responseMessage.ADD_SUCCESS("Appointment"),
data: bookTimeSlot,
});
}
}
catch(err){
await setResponseObject(req, false, err.message, "");
next();
}
}

module.exports = _appointment;
// let fromTime = req.body.timeSlot[0];
// let uptoTime = req.body.timeSlot[1];

// let hour1 = fromTime.slice(0,2);
// let minute1 = fromTime.slice(3);

// let hour2 = uptoTime.slice(0,2);
// let minute2 = uptoTime.slice(3);

// let date1 = new Date(dateString);
// date.setHours(hour1, minute1);

// let date2 = new Date(dateString);
// date2.setHours(hour2, minute2);

// let currentTime = new Date();
// currentTime.setHours(req.body.timeSlot[0]);
// currentTime.setMinutes(req.body.timeSlot[1]);
----------------------------------
const mongoose = require("mongoose");

var Appointment = new mongoose.Schema(
{
bookUser: { //expert
type: mongoose.Schema.Types.ObjectId,
ref: "UserProfile",
},
bookedBy: { //user
type: mongoose.Schema.Types.ObjectId,
ref: "UserProfile",
},
createdBy: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
duration: {
type: String,
},
amount : {
type : String,
},
status: {
type: String,
enum: ["Booked", "Completed", "Cancelled","Pending","Rejected"],
default: "Booked",
},
},
{ timestamps: true }
);

var freeTimeSlot = new mongoose.Schema({
userId:{
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
profileId:{
type: mongoose.Schema.Types.ObjectId,
ref: "UserProfile",
},
Date:{
type:Date,
default : Date.now
},
// fromTime:{
// type:Date,
// default : Date.now
// },
// uptoTime:{
// type:Date,
// default : Date.now

// },
from_Time : {
type : Date,
},
upto_Time : {
type : Date,
},
country : {
type : String,
}

});


module.exports.Appointment = mongoose.model("Appointment", Appointment);
module.exports.freeTimeSlot = mongoose.model('freeTimeSlot', freeTimeSlot);
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.