vendredi 5 juillet 2019

email not send correctly when two or more user click submit at the same time

i want so send email notification about rejected item every user click submit button and run ajax call to java server, but when two or more user click submit button at the same time, the email not send to each user but only the last user.

when the user click approve (submit) it will run : $('#submitApprove').click( function() {

        $('#submitApprove').prop("disabled", true).addClass("disabled");
        var twoToneButton = document.querySelector('.twoToneButton');

        twoToneButton.classList.add('spinning');

        var table = document.getElementById("pDetailTable");

        var count = 0;
        var rows = table.rows.length - 1;
        var start = 0;

        var arA = "";
        var arB = "";
        var arC = "";
        var arD = "";
        var arE = "";
        var arF = "";
        arG = "";

        for (var i = 1; i < table.rows.length; i++) {

            var a = table.rows[i].cells[6].innerHTML;
            var b = table.rows[i].cells[1].innerHTML;
            var c = table.rows[i].cells[5].children[0].value;
            var d = document.getElementById('site_rcv').value;
            var e = table.rows[i].cells[6].innerHTML;
            var f = document.getElementById('site_trf').value;
            var g = table.rows[i].cells[4].innerHTML;

            if (c >= 0) {
                if (c == "") {
                    c = table.rows[i].cells[4].innerHTML;
                }

                if (i == rows) {
                    arA += a;
                    arB += b;
                    arC += c;
                    arD += d;
                    arE += e;
                    arF += f;
                    arG += g;
                } else {
                    arA += a + ",";
                    arB += b + ",";
                    arC += c + ",";
                    arD += d + ",";
                    arE += e + ",";
                    arF += f + ",";
                    arG += g + ",";
                }

            }
            count++;
        }

        if (count == rows) {

            $.ajax({
                type : "POST",
                url : window.contextRoot + "/rcv/" + arB + "/" + arC + "/"
                        + arA + "/" + d + "/" + arG + "?trf_id="
                        + window.trf_id + "&from_site=" + f,
                success : function(data) {
                    twoToneButton.classList.remove('spinning');
                    if (data == true) {
                        var sup = '';
                        sup = window.contextRoot + '/rcv/all?success=true';
                        window.location = sup
                    }
                }
            });

        }

    })

this is a code from java controller :

@RequestMapping(value="/{proid}/{qty}/{stockid}/{sid}/{trfqty}")
public String add(@RequestParam(name = "trf_id") int trf_id,
        @RequestParam(name = "from_site") int from_site,
        @PathVariable String proid,
        @PathVariable String qty,
        @PathVariable String stockid,
        @PathVariable int sid,
        @PathVariable String trfqty,
        UserModel um) {
    auth();
    // non loop sid & trf_id
    ArrayList<String> pid= new ArrayList<String>(Arrays.asList(proid.split(",")));
    ArrayList<String> qtys= new ArrayList<String>(Arrays.asList(qty.split(",")));
    ArrayList<String> stkid= new ArrayList<String>(Arrays.asList(stockid.split(",")));
    ArrayList<String> tfqty= new ArrayList<String>(Arrays.asList(trfqty.split(",")));

    product_stock ps;
    transfer trf;
    product p;
    employee emp;
    Date now = new Date();
    int sendEmail = 0;
    String html="";


    if(u == true) {
        for(int i=0;i<pid.size();i++)
        {
            now = new Date();
            transfer_detail sp = trfDAO.getOneDetail(trf_id, Integer.parseInt(stkid.get(i)));
            if(sp != null) {
                site s = site.getOne(sid);
                if(s.getTypes() == true) {
                    ps = proDAO.getOneStock(sid,Integer.parseInt(pid.get(i)));
                    if(ps != null) {
                        //plus stock
                        ps.setStock(ps.getStock()+Integer.parseInt(qtys.get(i)));
                        ps.setFrom_site(from_site);
                        ps.setTrf_id(trf_id);;
                        ps.setUpdate_at(now);
                        ps.setUpdate_by(String.valueOf(um.getEmployee_id()));
                        proDAO.updateStock(ps);
                        //minus stock
                        ps = proDAO.getOnestkWH(Integer.parseInt(stkid.get(i)));
                        s = site.getOne(ps.getSite_id());
                        if(s.getTypes() == false) {
                            int tstock = ps.getStock()-Integer.parseInt(qtys.get(i));
                            if(tstock == 0) {
                                ps.setActive(false);
                            }
                        }
                        ps.setStock(ps.getStock()-Integer.parseInt(qtys.get(i)));
                        ps.setUpdate_at(now);
                        ps.setUpdate_by(String.valueOf(um.getEmployee_id()));
                        proDAO.updateStock(ps);
                    }else {
                        //plus stock
                        ps = new product_stock();
                        ps.setPro_id(Integer.parseInt(pid.get(i)));
                        ps.setSite_id(sid);
                        ps.setSup_id(0);
                        ps.setPrc_id(0);
                        ps.setFrom_site(from_site);
                        ps.setTrf_id(trf_id);;
                        ps.setStock(Integer.parseInt(qtys.get(i)));
                        ps.setDate_stock(now);
                        ps.setCreate_at(now);
                        ps.setCreate_by(String.valueOf(um.getEmployee_id()));
                        proDAO.addStock(ps);
                        //minus stock
                        ps = proDAO.getOnestkWH(Integer.parseInt(stkid.get(i)));
                        ps.setStock(ps.getStock()-Integer.parseInt(qtys.get(i)));
                        if(s.getTypes() == false) {
                            int tstock = ps.getStock()-Integer.parseInt(qtys.get(i));
                            if(tstock == 0) {
                                ps.setActive(false);
                            }
                        }
                        ps.setUpdate_at(now);
                        ps.setUpdate_by(String.valueOf(um.getEmployee_id()));
                        proDAO.updateStock(ps);
                    }
                }else {
                    //plus stock
                    if(Integer.parseInt(qtys.get(i)) != 0) {
                        ps = new product_stock();
                        ps.setPro_id(Integer.parseInt(pid.get(i)));
                        ps.setSite_id(sid);
                        ps.setSup_id(0);
                        ps.setPrc_id(0);
                        ps.setFrom_site(from_site);
                        ps.setTrf_id(trf_id);;
                        ps.setFrom_site(from_site);
                        ps.setTrf_id(trf_id);
                        ps.setStock(Integer.parseInt(qtys.get(i)));
                        ps.setDate_stock(now);
                        ps.setCreate_at(now);
                        ps.setCreate_by(String.valueOf(um.getEmployee_id()));
                        proDAO.addStock(ps);
                    }   
                    //minus stock

                    ps = proDAO.getOnestkWH(Integer.parseInt(stkid.get(i)));
                    ps.setStock(ps.getStock()-Integer.parseInt(qtys.get(i)));
                    ps.setUpdate_at(now);
                    int tstock = ps.getStock()-Integer.parseInt(qtys.get(i));
                    if(tstock == 0) {
                        ps.setActive(false);
                    }
                    ps.setUpdate_by(String.valueOf(um.getEmployee_id()));
                    proDAO.updateStock(ps);

                }

                sp.setUpdate_at(now);
                sp.setUpdate_by(String.valueOf(um.getEmployee_id()));
                sp.setReceive(true);
                sp.setActive(false);
                sp.setReceive_qty(Integer.parseInt(qtys.get(i)));
                trfDAO.updateDetail(sp);    

            }else {
                return "redirect:/error?text=data not found";
            }
            if(Integer.parseInt(tfqty.get(i)) > Integer.parseInt(qtys.get(i))) {
                sendEmail++;
                p = proDAO.getOne(Integer.parseInt(pid.get(i)));
                html += generateHTML(p.getPhoto(), p.getName(), tfqty.get(i), qtys.get(i));;
            }
        }
        if(sendEmail != 0) {
            trf = trfDAO.getOne(trf_id);
            site s = site.getOne(sid);
            emp = empDAO.getOne(Integer.parseInt(trf.getCreate_by()));
            try {
                trf.setStatus(3);
                trf.setUpdate_at(now);
                trf.setUpdate_by(String.valueOf(um.getEmployee_id()));      
                trfDAO.update(trf);
                SendEmail.generateAndSendEmail(emp.getEmail(), "Rejected item from "+s.getName(), SendEmail.Reject(html,trf.getRef_no()));
            } catch (AddressException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }else {
            trf = trfDAO.getOne(trf_id);
            trf.setStatus(2);
            trf.setUpdate_at(now);
            trf.setUpdate_by(String.valueOf(um.getEmployee_id()));

            trfDAO.update(trf);
        }
        return "redirect:/api/msg/success";
    }else {
        return "redirect:/error?text=you're not autorize to do this action";
    }


}




Aucun commentaire:

Enregistrer un commentaire