Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var filesModel = require('../../files/models/index');
- var testimonialModel = require('../../testimonials/models/index');
- var moveFileTmpToCdn = function (data, oldPath, newPath, cb) {
- Utils.fs.rename(oldPath, newPath, function (err, res) {
- if (err) {
- Utils.universalFunctions.logger("Step 3 error in moving file");
- if (err.code == 'EXDEV') {
- var execStatement = 'mv ' + oldPath + ' ' + newPath;
- Utils.exec(execStatement, function (err) {
- if (err) {
- Utils.universalFunctions.logger("Step 3 error in moving file using exec");
- cb(err)
- } else {
- Utils.universalFunctions.logger("Step 3 file moved succesfully");
- filesModel.Files.update({ _id: data }, { tmp_file: false }, function (err, res) {
- if (err) {
- Utils.universalFunctions.logger("Step 3 error in updating the file status in the database");
- cb(err)
- } else {
- Utils.universalFunctions.logger("Step 3 updated the file status in the database");
- cb(null, res)
- }
- })
- }
- })
- } else {
- cb(err)
- }
- } else {
- Utils.universalFunctions.logger("Step 3 file moved succesfully");
- filesModel.Files.update({ _id: data }, { tmp_file: false }, function (err, res) {
- if (err) {
- Utils.universalFunctions.logger("Step 3 for file status in the database");
- cb(err)
- } else {
- Utils.universalFunctions.logger("Step 3 for status in the database");
- cb(null, res)
- }
- })
- }
- })
- }
- /*********************************************************************************/
- {
- //************* api for upadting testimonial *********//
- method: 'PUT',
- path: '/v1/admin/editTestimonial',
- config: {
- description: 'user edit testimonial API',
- notes: 'user edit testimonial API',
- tags: ['api'],
- pre: [
- { method: Utils.universalFunctions.verifyAdminLoginToken, assign: "userDetails" }
- ],
- validate: {
- payload: {
- testimonial_id: Utils.Joi.string().required().length(24).trim().label('Testimonial id'),
- message: Utils.Joi.string().required().label('message'),
- image:Utils.Joi.string().optional().allow('').label('image'),
- name:Utils.Joi.string().required().label('name')
- },
- headers: Utils.Joi.object({
- 'x-logintoken': Utils.Joi.string().required().trim()
- }).options({ allowUnknown: true }),
- },
- },
- handler: function(request, reply) {
- Service.editTestimonial(request.payload, function(err, res) {console.log("+++++++++++",res)
- if (err) {
- Utils.response.error(reply);
- } else {
- if (res.status == "warning") {
- Utils.response.warningAdmin(reply, res.message);
- } else {
- Utils.response.success(reply,res);
- }
- }
- })
- }
- },
- \***************************************************************************************************\
- editTestimonial: function (request, callback) {
- Utils.async.waterfall([
- function (cb) {//checking Testimonial exists
- var query = {
- _id: request.testimonial_id,
- is_deleted: false
- }
- testimonialModel.Testimonials.find(query, function (err, res) {
- if (err) {
- cb(err)
- }
- else if (res.length > 0) {
- cb(null, res)
- }
- else {
- cb({ status: "warning", message: "Testimonial not found." })
- }
- })
- },
- function (data, cb) {//updating and moving image to cdn folder
- console.log()
- if (request.image && request.image != "") {
- filesModel.Files.find({ _id: request.image, tmp_file: true }, function (err, res) {
- if (err) {
- cb(err)
- } else {
- if (res.length > 0) {
- var file = res;
- var oldPath = file[0].tmp_location + "/" + file[0]._id + "." + file[0].file_extension;
- var newPath = Utils.path.join(__dirname, "../../../assets/cdn/" + file[0]._id + "/" + file[0]._id + "." + file[0].file_extension);
- var path = Utils.path.join(__dirname, "../../../assets/cdn/" + file[0]._id);
- var mode = "0777";
- Utils.fs.mkdir(path, mode, function (err, res) {
- if (err) {
- if (err.code == 'EEXIST') {
- Utils.universalFunctions.logger("Step 3 for update profile cdn path already existed");
- console.log(oldPath, newPath, "oldPath, newPath,-------11-----")
- moveFileTmpToCdn(file, oldPath, newPath, function (err, res) {
- if (err) {
- cb(err)
- } else if (res) {
- filesModel.Files.findOne({ _id: file[0]._id }, (err, file_result) => {
- if (err) {
- cb(err);
- } else {
- fileName = file[0]._id + "." + file[0].file_extension;
- cb(null, file[0]._id);
- }
- })
- } else {
- }
- });
- } else {
- cb(err)
- }
- } else {
- Utils.universalFunctions.logger("Step 3 for update profile cdn path created succesfully");
- console.log(oldPath, newPath, "oldPath, newPath,------22------")
- moveFileTmpToCdn(file, oldPath, newPath, function (err, res) {
- if (err) {
- cb(err)
- } else if (res) {
- filesModel.Files.findOne({ _id: file[0]._id }, (err, file_result) => {
- if (err) {
- cb(err);
- } else {
- fileName = file[0]._id + "." + file[0].file_extension;
- cb(null, file[0]._id);
- }
- })
- // fileName = file[0]._id + "." + file[0].file_extension;
- // callback(null, data);
- } else {
- }
- });
- }
- })
- } else {
- cb({ status: "warning", message: "Unable to find the image media source" })
- }
- }
- });
- } else {
- cb(null, data);
- }
- },
- function (data, cb) {//updating Testimonial
- var query = {
- _id: request.testimonial_id
- }
- var objUpdate = {
- message: request.message,
- name: request.name
- }
- if (request.image && request.image != "") {
- objUpdate.image = data
- }
- testimonialModel.Testimonials.findOneAndUpdate(query, objUpdate, { new: true }, function (err, res) {
- if (err) {
- cb(err)
- }
- else { console.log("====TESTIMONIAL UPDATED =====", res)
- res = "Testimonial updated successfully."
- cb(null, res)
- }
- })
- }
- ], function (err, result) {
- if (err) {
- if (err.status == "warning") {
- callback(null, err)
- }
- else {
- callback(err)
- }
- } else {
- callback(null, result)
- }
- })
- }
Add Comment
Please, Sign In to add comment