UNPKG

flexbiz-server

Version:

Flexible Server

9 lines (8 loc) 3.61 kB
'use strict';const model=global.getModel("assprofitsharing"),assproduct=global.getModel("assproduct"),Trangthai=global.getModel("trangthai"),controller=require("../../controllers/controller"),{isSupperAdmin}=require("../../libs/utils"); module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,"assprofitsharing",{sort:{hoat_dong:-1,date_created:-1},notNeedRight:!0,onFinding:async($pros_user$$,$condition$$,$next$$)=>{$condition$$.ma_loai&&($pros_user$$=await assproduct.find({id_app:$pros_user$$.current_id_app,ma_loai:$condition$$.ma_loai,status:!0},{id:1}),$condition$$.id_product={$in:$pros_user$$.map($p$$=>$p$$.id)},delete $condition$$.ma_loai);$next$$(null,$condition$$)},onView:async($user$$, $items$$,$next$$)=>{await $items$$.asyncJoinModel2($user$$.current_id_app,assproduct,{where:{id_product:"id"},fields:["ten_san_pham","hinh_anh","logo","ma_loai",{product_token:"token"},"gia_tri_hien_tai","sl_nhan_vien","nganh_nghe"]});$items$$.joinModel2(null,Trangthai,[{where:$a$$=>({ma_trang_thai:$a$$.trang_thai,ma_ct:"PROFITSHARING"}),fields:["ten_trang_thai","color"]}],function(){$next$$(null,$items$$)})},onCreating:async($user$$,$obj$$,$next$$)=>{const $id_app$$=$user$$.current_id_app;if(!await global.getModel("asskey").findOne({id_app:$user$$.current_id_app, username:$user$$.email,address:$obj$$.creator}).lean())return $next$$("\u0110\u1ecba ch\u1ec9 v\u00ed n\u00e0y kh\u00f4ng thu\u1ed9c v\u1ec1 b\u1ea1n");if(!await global.getModel("assproduct").findOne({id_app:$id_app$$,id:$obj$$.id_product}).lean())return $next$$("D\u1ef1 \u00e1n kh\u00f4ng t\u1ed3n t\u1ea1i");if(100<$obj$$.ty_le_chia_theo_thoi_gian)return $next$$("T\u1ef7 l\u1ec7 chia theo th\u1eddi gian kh\u00f4ng \u0111\u01b0\u1ee3c l\u1edbn h\u01a1n 100%");$next$$(null,$obj$$)},onCreated:async($user$$, $obj$$,$next$$)=>{$obj$$.id=$obj$$._id.toString();await model.updateOne({_id:$obj$$._id},{id:$obj$$.id});$next$$(null,$obj$$)}});$contr_router$$.deleting=async function($user$$,$obj$$,$fn$$){if(!isSupperAdmin($user$$.email.toLowerCase()))return $fn$$("B\u1ea1n kh\u00f4ng c\u00f3 quy\u1ec1n x\u00f3a");if(1==$obj$$.da_chia_loi_nhuan)return $fn$$("Kh\u00f4ng th\u1ec3 xo\u00e1 y\u00eau c\u1ea7u n\u00e0y do \u0111\u00e3 \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n");if(!await global.getModel("asskey").findOne({id_app:$user$$.current_id_app, username:$user$$.email,address:$obj$$.creator}).lean())return $fn$$("\u0110\u1ecba ch\u1ec9 v\u00ed n\u00e0y kh\u00f4ng thu\u1ed9c v\u1ec1 b\u1ea1n");$fn$$(null,$obj$$)};$contr_router$$.updating=function($user$$,$data$$,$obj$$,$fn$$){if($user$$.email!==$obj$$.user_created)return $fn$$("B\u1ea1n kh\u00f4ng c\u00f3 quy\u1ec1n c\u1eadp nh\u1eadt");if(1==$obj$$.da_chia_loi_nhuan)return $fn$$("Kh\u00f4ng th\u1ec3 c\u1eadp nh\u1eadt y\u00eau c\u1ea7u n\u00e0y do \u0111\u00e3 \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n"); if($data$$.creator&&$data$$.creator!==$obj$$.creator)return $fn$$("Kh\u00f4ng \u0111\u01b0\u1ee3c ph\u00e9p s\u1eeda \u0111\u1ecba ch\u1ec9 v\u00ed ng\u01b0\u1eddi t\u1ea1o");if($obj$$.da_chia_loi_nhuan)return $fn$$("Kh\u00f4ng th\u1ec3 c\u1eadp nh\u1eadt y\u00eau c\u1ea7u n\u00e0y do \u0111\u00e3 \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n");$data$$.ngay_chia_loi_nhuan=$data$$.ngay_chia_loi_nhuan||$obj$$.ngay_chia_loi_nhuan;$data$$.ty_le_chia_theo_thoi_gian=$data$$.ty_le_chia_theo_thoi_gian||$obj$$.ty_le_chia_theo_thoi_gian; if(100<$data$$.ty_le_chia_theo_thoi_gian)return $fn$$("T\u1ef7 l\u1ec7 chia theo th\u1eddi gian kh\u00f4ng \u0111\u01b0\u1ee3c l\u1edbn h\u01a1n 100%");$fn$$(null,$data$$,$obj$$)};$contr_router$$.route()};