Guest User

Untitled

a guest
Feb 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.86 KB | None | 0 0
  1. var filesModel = require('../../files/models/index');
  2. var testimonialModel = require('../../testimonials/models/index');
  3. var moveFileTmpToCdn = function (data, oldPath, newPath, cb) {
  4. Utils.fs.rename(oldPath, newPath, function (err, res) {
  5. if (err) {
  6. Utils.universalFunctions.logger("Step 3 error in moving file");
  7. if (err.code == 'EXDEV') {
  8.  
  9. var execStatement = 'mv ' + oldPath + ' ' + newPath;
  10.  
  11. Utils.exec(execStatement, function (err) {
  12. if (err) {
  13. Utils.universalFunctions.logger("Step 3 error in moving file using exec");
  14. cb(err)
  15. } else {
  16. Utils.universalFunctions.logger("Step 3 file moved succesfully");
  17. filesModel.Files.update({ _id: data }, { tmp_file: false }, function (err, res) {
  18. if (err) {
  19. Utils.universalFunctions.logger("Step 3 error in updating the file status in the database");
  20. cb(err)
  21. } else {
  22. Utils.universalFunctions.logger("Step 3 updated the file status in the database");
  23. cb(null, res)
  24. }
  25. })
  26. }
  27. })
  28. } else {
  29. cb(err)
  30. }
  31.  
  32. } else {
  33. Utils.universalFunctions.logger("Step 3 file moved succesfully");
  34. filesModel.Files.update({ _id: data }, { tmp_file: false }, function (err, res) {
  35. if (err) {
  36. Utils.universalFunctions.logger("Step 3 for file status in the database");
  37. cb(err)
  38. } else {
  39. Utils.universalFunctions.logger("Step 3 for status in the database");
  40. cb(null, res)
  41. }
  42. })
  43. }
  44. })
  45. }
  46. /*********************************************************************************/
  47. {
  48. //************* api for upadting testimonial *********//
  49. method: 'PUT',
  50. path: '/v1/admin/editTestimonial',
  51. config: {
  52. description: 'user edit testimonial API',
  53. notes: 'user edit testimonial API',
  54. tags: ['api'],
  55. pre: [
  56. { method: Utils.universalFunctions.verifyAdminLoginToken, assign: "userDetails" }
  57. ],
  58. validate: {
  59. payload: {
  60. testimonial_id: Utils.Joi.string().required().length(24).trim().label('Testimonial id'),
  61. message: Utils.Joi.string().required().label('message'),
  62. image:Utils.Joi.string().optional().allow('').label('image'),
  63. name:Utils.Joi.string().required().label('name')
  64.  
  65. },
  66. headers: Utils.Joi.object({
  67. 'x-logintoken': Utils.Joi.string().required().trim()
  68. }).options({ allowUnknown: true }),
  69. },
  70. },
  71. handler: function(request, reply) {
  72. Service.editTestimonial(request.payload, function(err, res) {console.log("+++++++++++",res)
  73. if (err) {
  74. Utils.response.error(reply);
  75. } else {
  76. if (res.status == "warning") {
  77. Utils.response.warningAdmin(reply, res.message);
  78. } else {
  79. Utils.response.success(reply,res);
  80. }
  81. }
  82. })
  83. }
  84. },
  85. \***************************************************************************************************\
  86. editTestimonial: function (request, callback) {
  87. Utils.async.waterfall([
  88. function (cb) {//checking Testimonial exists
  89. var query = {
  90. _id: request.testimonial_id,
  91. is_deleted: false
  92. }
  93. testimonialModel.Testimonials.find(query, function (err, res) {
  94. if (err) {
  95. cb(err)
  96. }
  97. else if (res.length > 0) {
  98. cb(null, res)
  99. }
  100. else {
  101. cb({ status: "warning", message: "Testimonial not found." })
  102. }
  103. })
  104. },
  105. function (data, cb) {//updating and moving image to cdn folder
  106.  
  107. console.log()
  108. if (request.image && request.image != "") {
  109. filesModel.Files.find({ _id: request.image, tmp_file: true }, function (err, res) {
  110. if (err) {
  111. cb(err)
  112. } else {
  113. if (res.length > 0) {
  114. var file = res;
  115.  
  116. var oldPath = file[0].tmp_location + "/" + file[0]._id + "." + file[0].file_extension;
  117. var newPath = Utils.path.join(__dirname, "../../../assets/cdn/" + file[0]._id + "/" + file[0]._id + "." + file[0].file_extension);
  118. var path = Utils.path.join(__dirname, "../../../assets/cdn/" + file[0]._id);
  119. var mode = "0777";
  120.  
  121. Utils.fs.mkdir(path, mode, function (err, res) {
  122. if (err) {
  123. if (err.code == 'EEXIST') {
  124. Utils.universalFunctions.logger("Step 3 for update profile cdn path already existed");
  125. console.log(oldPath, newPath, "oldPath, newPath,-------11-----")
  126. moveFileTmpToCdn(file, oldPath, newPath, function (err, res) {
  127. if (err) {
  128. cb(err)
  129. } else if (res) {
  130.  
  131. filesModel.Files.findOne({ _id: file[0]._id }, (err, file_result) => {
  132. if (err) {
  133. cb(err);
  134. } else {
  135. fileName = file[0]._id + "." + file[0].file_extension;
  136. cb(null, file[0]._id);
  137. }
  138. })
  139.  
  140. } else {
  141.  
  142. }
  143. });
  144. } else {
  145. cb(err)
  146. }
  147. } else {
  148. Utils.universalFunctions.logger("Step 3 for update profile cdn path created succesfully");
  149. console.log(oldPath, newPath, "oldPath, newPath,------22------")
  150. moveFileTmpToCdn(file, oldPath, newPath, function (err, res) {
  151. if (err) {
  152. cb(err)
  153. } else if (res) {
  154.  
  155. filesModel.Files.findOne({ _id: file[0]._id }, (err, file_result) => {
  156. if (err) {
  157. cb(err);
  158. } else {
  159. fileName = file[0]._id + "." + file[0].file_extension;
  160. cb(null, file[0]._id);
  161. }
  162. })
  163. // fileName = file[0]._id + "." + file[0].file_extension;
  164. // callback(null, data);
  165. } else {
  166.  
  167. }
  168. });
  169. }
  170. })
  171. } else {
  172. cb({ status: "warning", message: "Unable to find the image media source" })
  173. }
  174. }
  175. });
  176.  
  177. } else {
  178. cb(null, data);
  179. }
  180. },
  181.  
  182. function (data, cb) {//updating Testimonial
  183. var query = {
  184. _id: request.testimonial_id
  185. }
  186. var objUpdate = {
  187. message: request.message,
  188. name: request.name
  189. }
  190. if (request.image && request.image != "") {
  191. objUpdate.image = data
  192. }
  193. testimonialModel.Testimonials.findOneAndUpdate(query, objUpdate, { new: true }, function (err, res) {
  194. if (err) {
  195. cb(err)
  196. }
  197. else { console.log("====TESTIMONIAL UPDATED =====", res)
  198.  
  199. res = "Testimonial updated successfully."
  200. cb(null, res)
  201. }
  202. })
  203. }
  204. ], function (err, result) {
  205. if (err) {
  206. if (err.status == "warning") {
  207. callback(null, err)
  208. }
  209. else {
  210. callback(err)
  211. }
  212. } else {
  213. callback(null, result)
  214. }
  215. })
  216. }
Add Comment
Please, Sign In to add comment