UNPKG

flexbiz-server

Version:

Flexible Server

31 lines (30 loc) 2.41 kB
'use strict';const subscribe=global.getModel("subscribe"),crypto=require("crypto"); module.exports=function($router$$){$router$$.route("/subscribe").post(function($req$$,$res$$){const $body$$=$req$$.body;if(!$body$$)return $res$$.status(411).send("Kh\u00f4ng c\u00f3 d\u1eef li\u1ec7u");subscribe.findOne({email:$body$$.email},function($e$$,$rs$$){if($rs$$)return $res$$.status(400).send("Email n\u00e0y \u0111\u00e3 \u0111\u01b0\u1ee3c \u0111\u0103ng k\u00fd");(new subscribe($body$$)).save(function($error$$){if($error$$)return $res$$.status(400).send("Kh\u00f4ng th\u1ec3 \u0111\u0103ng k\u00fd\n"+$error$$); $res$$.send("B\u1ea1n \u0111\u00e3 \u0111\u0103ng k\u00fd th\u00e0nh c\u00f4ng")})})});$router$$.route("/unsubscribe/:encrypted").get(async function($req$$,$res$$){try{let $encrypted$$=$req$$.params.encrypted;const $decipher$$=crypto.createDecipher("aes192",configs.cryptoPassword||"PVT@1");let $decrypted$$=$decipher$$.update($encrypted$$,"hex","utf8");$decrypted$$+=$decipher$$.final("utf8");let $data$$=JSON.parse($decrypted$$),{id_app:$id_app$$,email:$email$$}=$data$$;if(!$id_app$$||!$email$$||!global.mongoose.Types.ObjectId.isValid($id_app$$))return $res$$.status(400).send("Link is not valid"); let $appInfo$$=await global.getModel("app").findOne({_id:$id_app$$}).lean()||{};subscribe.findOneAndUpdate({email:$email$$,id_app:$id_app$$},{unsubcribe:!0},{new:!0,upsert:!0},function($e$$){if($e$$)return $res$$.status(400).send($e$$.message||$e$$.error||$e$$);$res$$.send(` <html> <title>${$appInfo$$.name||"unsubscribe"}</title> <head> </head> <body style="background-color:#eeeeee"> <div style=" width: 600px; margin: 0 auto 10px auto; text-align: left;" > <div style=" line-height: 150%; font-family: Helvetica; font-size: 14px; color: #333333; padding: 20px; border-radius: 6px; background-color: #ffffff;" > <h3>H\u1ee7y \u0111\u0103ng k\u00fd th\u00e0nh c\u00f4ng</h3> <p>B\u1ea1n s\u1ebd kh\u00f4ng c\u00f2n nh\u1eadn \u0111\u01b0\u1ee3c email ti\u1ebfp th\u1ecb t\u1eeb danh s\u00e1ch n\u00e0y.</p> </div> </div> </body> </html> `)})}catch($e$$){return $res$$.status(400).send($e$$.message||$e$$.error||$e$$)}})};