pfitzpaddy / ngm-reportDesk

The workdesk for ReportHub
1 stars 6 forks source link

May 2020 activity reports duplication #364

Closed drfaustusfade closed 4 years ago

drfaustusfade commented 4 years ago

some May reports 2020 are duplicated it could be that setReportsToDo run twice close in time, so that second find report query would not find and proceed to report creation

// check duplicates 
db.getCollection("report").aggregate([
        { $match: { report_month: 4, report_year: 2020 } },
        { $group: { _id: { project_id: "$project_id", admin0pcode: "$admin0pcode" }, count: { $sum: 1 } } },
        { $match: { count: { $gt: 1 } } }
]);

// find projects with dublicates
// check if report contains beneficiaries
// remove second report and its locations if no beneficiaries
db.getCollection("report").aggregate([
    { $match: { report_month: 4, report_year: 2020 } },
    { $group: { _id: { project_id: "$project_id", admin0pcode: "$admin0pcode" }, count: { $sum: 1 } } },
    { $match: { count: { $gt: 1 } } }]).forEach(function (d) {
        print("project: ", d._id.project_id);
        reports = db.getCollection("report").find({ project_id: d._id.project_id, report_month: 4, report_year: 2020 });
        count = 0;
        reportsArr = [];
        reportHasBeneficiary = [];
        reports.forEach(function (r) {
            count += 1;
            reportsArr.push(r._id);
            print(count, " report: ", r._id, r.createdAt);
            b = db.getCollection("beneficiaries").findOne({ report_id: r._id.valueOf() })
            if (b) print('beneficiary: ', b._id, b.report_id, b.admin0pcode);
            if (b) reportHasBeneficiary[count - 1] = true;
            if (!b) reportHasBeneficiary[count - 1] = false
            if (!b && count > 1) {
                // delete report, locations
                db.getCollection("report").remove({ _id: r._id }, true);
                db.getCollection("location").remove({ report_id: r._id.valueOf() });
            }
            if (b && count > 1) {
                if (reportHasBeneficiary[0] === false) {
                    db.getCollection("report").remove({ _id: reportsArr[0] }, true);
                    db.getCollection("location").remove({ report_id: reportsArr[0].valueOf() });
                }
            }
            print(reportHasBeneficiary);
        });
    });