File: /var/www/api.javaapp.co.uk/src/middleware/validator.js
import exp from "constants";
import niv from "node-input-validator";
export async function createUserValidator(req, res, next) {
const objValidation = new niv.Validator(req.body, {
name: "required|string",
email: "required|email",
password: "required|string",
});
const matched = await objValidation.check();
if (!matched) {
return res.status(422).send({
status: 422,
message: "Validation Error",
error: objValidation.errors,
});
} else {
next();
}
}
export async function loginValidator(req, res, next) {
const objValidation = new niv.Validator(req.body, {
email: 'required|email',
password: 'required|string',
device_id: 'required|string',
device_type: 'required|integer',
fcm_token: 'required|string'
})
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function otpVerifyValidator(req, res, next) {
const objValidation = new niv.Validator(req.body, {
email: 'required|email',
otp: 'required'
})
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function sendOtpValidator(req, res, next) {
const objValidation = new niv.Validator(req.body, {
email: 'required|email',
})
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function s3BucketDetailsValidator(req, res, next) {
const objValidation = new niv.Validator(req.query, {
file_type: 'required|string',
})
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function updateUserValidator(req, res, next) {
const objValidation = new niv.Validator(req.body, {
name: 'required|string',
})
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function updateFcmValidator(req, res, next) {
const objValidation = new niv.Validator(req.body, {
fcm_token: 'required|string',
})
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function deleteUser(req, res, next) {
const objValidation = new niv.Validator(req.body, {
delete_reason: 'required|string',
})
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function appVersionValidator(req, res, next) {
const objValidation = new niv.Validator(req.query, {
os_type: 'required|integer',
})
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function notificationValidator(req, res, next) {
const objValidation = new niv.Validator(req.body, {
notification_status: 'required|integer',
})
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function getInHouseMenu(req, res, next) {
const objValidation = new niv.Validator(req.body, {
page_no: 'required|integer',
})
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function GetInHouseItemById(req, res, next) {
const objValidation = new niv.Validator(req.body, {
item_id: 'required|integer',
})
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function saveOrder(req, res, next) {
const objValidation = new niv.Validator(req.body, {
items: 'required|array'
})
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
//Delete Item in In house Order
export async function deleteInhouseItem(req, res, next) {
const objValidation = new niv.Validator(req.body, {
item_id: 'required|integer'
})
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// create Group
export async function createGroup(req, res, next) {
const objValidation = new niv.Validator(req.body, {
group_name: 'required|string',
///TODO need to fix this while making the code live
//group_profile: 'required|string',
// group_members: 'required|array',
latitude: 'required|string',
longitude: 'required|string',
group_cafes: 'required|array'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function getCafeList(req, res, next) {
const objValidation = new niv.Validator(req.body, {
page: 'required|integer',
group_id: 'required|integer',
latitude: 'required|numeric',
longitude: 'required|numeric'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function GetGroupDetails(req, res, next) {
const objValidation = new niv.Validator(req.body, {
group_id: 'required|integer',
latitude: 'required|string',
longitude: 'required|string'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function GroupDetails(req, res, next) {
const objValidation = new niv.Validator(req.body, {
group_id: 'required|integer',
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function getPeopleList(req, res, next) {
const objValidation = new niv.Validator(req.query, {
page: "required|integer",
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function getSearchPeopleList(req, res, next) {
const objValidation = new niv.Validator(req.query, {
page: "required|integer",
user_name: 'required|string',
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// add additional cafe in group
export async function addCafe(req, res, next) {
const objValidation = new niv.Validator(req.body, {
group_id: 'required|integer',
group_cafes: 'required|array'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function removeUser(req, res, next) {
const objValidation = new niv.Validator(req.body, {
group_id: 'required|integer',
user_id: 'required|integer'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// cafe category list validation
export async function cafeCategoryList(req, res, next) {
const objValidation = new niv.Validator(req.query, {
cafe_id: 'required|integer',
page: 'required|integer'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// cafe category list validation
export async function cafeListValidator(req, res, next) {
const objValidation = new niv.Validator(req.body, {
latitude: 'required|string',
longitude: 'required|string',
page: 'required|integer',
filter_flag: 'required|integer'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// search cafe validation
export async function searchCafeListValidator(req, res, next) {
const objValidation = new niv.Validator(req.body, {
latitude: 'required|string',
longitude: 'required|string',
page: 'required|integer',
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// search cafe validation by cafe name
export async function searchByCafeNameValidator(req, res, next) {
const objValidation = new niv.Validator(req.body, {
latitude: 'required|string',
longitude: 'required|string',
cafe_name: 'required|string',
page: 'required|integer',
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// cafe favorite validation
export async function cafeFavoriteValidation(req, res, next) {
const objValidation = new niv.Validator(req.body, {
cafe_id: 'required|integer',
is_favorite: 'required|integer'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// request cafe validation
export async function cafeRequestValidation(req, res, next) {
const objValidation = new niv.Validator(req.body, {
cafe_name: 'required|string',
postcode: 'required|string',
//email: 'required|email',
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// get cafe Item list validation
export async function cafeItemList(req, res, next) {
const objValidation = new niv.Validator(req.query, {
menu_id: 'required|integer',
page: 'required|integer',
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function addgroupcode(req, res, next) {
const objValidation = new niv.Validator(req.body, {
group_code: 'required|numeric|maxLength:6',
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// get cafe details
export async function cafeDetailValidation(req, res, next) {
const objValidation = new niv.Validator(req.body, {
cafe_id: 'required|integer',
user_latitude: 'required|string',
user_longitude: 'required|string'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// get cafe item details
export async function cafeItemDetailValidation(req, res, next) {
const objValidation = new niv.Validator(req.body, {
item_id: 'required|integer',
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// suggested cafe item list
export async function cafeSuggestedItemValidation(req, res, next) {
const objValidation = new niv.Validator(req.body, {
item_id: 'required',
page: 'required',
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// add members in group
export async function addGroupMember(req, res, next) {
const objValidation = new niv.Validator(req.body, {
group_id: 'required|integer',
members: 'required|array',
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// save group coffee run
export async function saveGroupCoffeeRun(req, res, next) {
const validationRules = {
group_id: 'required|integer',
cafe_id: 'integer',
request_endtime: 'required|integer',
users: 'required|array',
};
if (req.body.type === 1) {
validationRules.cafe_id = 'required|integer';
}
const objValidation = new niv.Validator(req.body, validationRules);
const matched = await objValidation.check();
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// get group coffee run data
export async function getGroupCoffeeRun(req, res, next) {
const objValidation = new niv.Validator(req.query, {
group_id: 'required|integer'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// place order validation check
export async function placeOrder(req, res, next) {
const objValidation = new niv.Validator(req.body, {
cafe_id: 'required|integer',
is_main_order: 'required|integer|in:0,1',
order_item_array: 'required|array',
items_amount: 'required|numeric'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function individualOrderList(req, res, next) {
const objValidation = new niv.Validator(req.query, {
order_id: 'required|integer',
cafe_id: 'required|integer'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// save order
export async function cafeSaveOrder(req, res, next) {
const objValidation = new niv.Validator(req.body, {
cafe_id: 'required|integer',
is_main_order: 'required|integer|in:0,1',
order_item_array: 'required|array',
total_amount: 'required|numeric'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// change order status
export async function changeOrderStatus(req, res, next) {
const objValidation = new niv.Validator(req.body, {
flag: 'required|integer|in:1,2'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// check redeem
export async function redeemValidation(req, res, next) {
const objValidation = new niv.Validator(req.query, {
cafe_id: 'required|integer'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
// get order by group ID
export async function orderListByGroup(req, res, next) {
const objValidation = new niv.Validator(req.query, {
group_id: 'required|integer',
page: "required|integer"
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
//get group coffee run orders
export async function getGroupCoffeeRunOrders(req, res, next) {
if (!req.body.group_coffee_run_id && !req.body.group_id) {
return res.status(422).send({
status: 422,
message: "Validation Error",
error: "At least one of 'group_coffee_run_id' or 'group_id' is required"
});
}
const objValidation = new niv.Validator(req.body, {
group_coffee_run_id: 'requiredIf:group_id,=""',
group_id: 'requiredIf:group_coffee_run_id,=""'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
req.group_coffee_run_id = req.body.group_coffee_run_id;
next();
}
}
//Mark order Collected
export async function markOrderCollected(req, res, next) {
const objValidation = new niv.Validator(req.body, {
group_coffee_run_id: 'required',
type: "required"
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
req.group_coffee_run_id = req.body.group_coffee_run_id;
next();
}
}
export async function createCafeGoToOrderValidation(req, res, next) {
const objValidation = new niv.Validator(req.body, {
cafe_id: 'required',
order_items: 'required'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}
export async function updateCafeGoToOrderValidation(req, res, next) {
const objValidation = new niv.Validator(req.body, {
id: 'required',
order_items: 'required'
});
const matched = await objValidation.check()
if (!matched) {
return res
.status(422)
.send({
status: 422,
message: "Validation Error",
error: objValidation.errors
})
} else {
next();
}
}