Files
WrenchBoradWeb/wrenchboard/src/shared_tool/site_crons.cc
T
2022-07-30 08:50:58 -04:00

313 lines
13 KiB
C++

// Site Crons Management Tools
#include "clog.h"
#include "cgi.h"
#include "input.h"
#include "wrenchboard_api.h"
#include "site_crons.h"
#include "email.h"
#include "safestring.h"
#include <string>
#include "pgsql.h"
#include "pgsql_wrapper.h"
#include "cfg.h"
#include <curl/curl.h>
#include "jobs.h"
long ReviewReminder(CVars in, CVars &out);
long OfferPending(CVars in, CVars &out);
long InterestPending(CVars in, CVars &out);
long PaymentPending(CVars in, CVars &out);
long DeadlineLineApprachClient(CVars in, CVars &out);
long SignUpPendingReminder(CVars in, CVars &out);
long SignUpStats(CVars in, CVars &out);
long PastDueWarings(CVars in, CVars &out);
long PastDueOffersRefund(CVars in, CVars &out);
long CronTracker(CVars in, CVars &out);
/*
CREATE TABLE cron_jobs (
id SERIAL,
job_type VARCHAR(50) UNIQUE NOT NULL,
description VARCHAR(55) NOT NULL,
added timestamp without time zone DEFAULT now(),
updated timestamp without time zone DEFAULT now(),
active INT DEFAULT 0
);
ALTER TABLE ONLY cron_jobs
ADD CONSTRAINT cron_jobs_id_key UNIQUE (id);
*/
long sitecrons_calls(CVars in, CVars &out) {
logfmt(logINFO, "sitecrons_calls()");
//out["result"] = "YES I GET TO BACK END";
long call_action = REQ_LONG(in, "call_action", 0, -1);
CVars x;
CVars ct,co;
long ret = 0;
const char * loc = getenv("REMOTE_ADDR");
logfmt(logINFO, "sitecrons_calls() action=%lu",call_action);
switch (call_action) {
case WRB_CRONJOB_JOBDUE_REMINDER:
ct["job_type"]="WRB_CRONJOB_JOBDUE_REMINDER"; ct["job_type"].set_valid( true );
ct["description"]="This is to organize and send job due date remider to client"; ct["description"].set_valid( true );
CronTracker(ct, co);
return DeadlineLineApprachClient(in, out);
break;
case WRB_CRONJOB_JOBDUE_PAYMENTS:
ct["job_type"]="WRB_CRONJOB_JOBDUE_PAYMENTS"; ct["job_type"].set_valid( true );
ct["description"]="This is to organize jonb compleyted remider to owner of task"; ct["description"].set_valid( true );
CronTracker(ct, co);
return PaymentPending(in, out);
break;
case WRB_CRONJOB_SIGNUP_ALERT:
ct["job_type"]="WRB_CRONJOB_SIGNUP_ALERT"; ct["job_type"].set_valid( true );
ct["description"]="General Signup job stats "; ct["description"].set_valid( true );
CronTracker(ct, co);
return SignUpStats(in, out);
break;
case WRB_CRONJOB_PASTDUE_ALERT:
ct["job_type"]="WRB_CRONJOB_PASTDUE_ALERT"; ct["job_type"].set_valid( true );
ct["description"]="This is to organize and send job due date remider to owner of task"; ct["description"].set_valid( true );
CronTracker(ct, co);
return PastDueWarings(in, out);
break;
case WRB_CRONJOB_OFFER_REFUND_ALERT:
ct["job_type"]="WRB_CRONJOB_OFFER_REFUND_ALERT"; ct["job_type"].set_valid( true );
ct["description"]="Offer expired , need to cancel and return deposit for the offer"; ct["description"].set_valid( true );
CronTracker(ct, co);
return PastDueOffersRefund(in, out);
break;
case WRB_CRONJOB_PENDOFFER_ALERT:
ct["job_type"]="WRB_CRONJOB_PENDOFFER_ALERT"; ct["job_type"].set_valid( true );
ct["description"]="Offer is pending remind the client to attend to it before it expires "; ct["description"].set_valid( true );
CronTracker(ct, co);
return OfferPending(in, out);
break;
}
logfmt(logINFO, "/sitecrons_calls()");
return ret;
}
long CronTracker(CVars in, CVars &out){
long ret = 0;
const PGresult *res;
try {
REQ_STRING(in, "job_type", 2, 149, "(.*)");
REQ_STRING(in, "description", 2, 149, "(.*)");
if ( load_db_record(out, "SELECT id AS cron_id,active FROM cron_jobs WHERE job_type ='%s' ", in["job_type"].c_str())> 0){
ret = out["active"].Long();
res = pgsql_query("UPDATE cron_jobs SET updated=now() WHERE id = %lu", out["cron_id"].Long());
}
else{
CVars xx;
xx["job_type"] = in["job_type"];
xx["job_type"].set_valid(true);
xx["description"] = in["description"];
xx["description"].set_valid(true);
long cron_id = insert_db_record(DBS_VALID, "cron_jobs", "cron_jobs_id_seq", xx);
}
} catch (bad_parameter) {
out["action_status"] = "Invalid CronTracker Action";
}
return ret;
}
long ReviewReminder(CVars in, CVars &out) {
}
long OfferPending(CVars in, CVars &out) {
logfmt(logINFO, "ENTER OfferPending()");
long limit = in["one_limit"].Long();
const PGresult *res = pgsql_query("SELECT jo.id AS offer_id,jo.added,j.title,jo.expire,jo.email, jo.client_id, "
"jo.job_id,jo.reminder_count,jo.reminder_date "
"FROM members_jobs_offer jo "
"LEFT JOIN members_jobs j ON j.id = jo.job_id "
"WHERE now() > jo.added +'6 hrs' "
"AND jo.expire > now() AND jo.status = 1 AND jo.public_view =0 "
"AND jo.reminder_count =0 AND jo.reminder_date IS NULL LIMIT %lu ",limit);
if (res != NULL && pgsql_num_rows(res) > 0) {
for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
if (f.empty()) continue;
CVars rec;
map_to_cvars(f, rec);
cron_email(WRB_CRONJOB_PENDOFFER_ALERT, rec, out); // send the cron email
pgsql_exec("UPDATE members_jobs_offer SET reminder_count=reminder_count+1, reminder_date=now() "\
"WHERE id=%lu AND job_id=%lu ", rec["offer_id"].Long(),rec["job_id"].Long());
}
}
logfmt(logINFO, "/OfferPending()");
return 0;
}
long InterestPending(CVars in, CVars &out) {
}
long PastDueOffersRefund(CVars in, CVars &out) {
logfmt(logINFO, "ENTER PastDueOffersRefund()");
long contract_id = 0;
const PGresult *res = pgsql_query("SELECT j.* "
" FROM members_jobs_offer j "
" LEFT JOIN members m1 ON m1.id=j.member_id "
" LEFT JOIN members m2 ON m2.id=j.client_id "
" WHERE j.expire < now() "
" AND j.status = 1 AND j.auto_close IS NULL "
" AND j.payment_id IS NOT NULL LIMIT 1");
if (res != NULL && pgsql_num_rows(res) > 0) {
for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
if (f.empty()) continue;
CVars rec;
map_to_cvars(f, rec);
rec["offer_result"] = OFFER_EXPIRE;
rec["offer_result"].set_valid( true );
WrenchConcludeJobsOffer(rec, out);
pgsql_exec("UPDATE members_jobs_offer SET auto_close=now() "\
"WHERE offer_code='%s' ", rec["offer_code"].c_str());
}
}
logfmt(logINFO, "/PastDueOffersRefund()");
return 0;
}
long PastDueWarings(CVars in, CVars &out) {
logfmt(logINFO, "ENTER PastDueWarings()");
long contract_id = 0;
const PGresult *res = pgsql_query("SELECT mc.id AS contract_id, mc.member_id,mc.auto_remind "
" FROM members_jobs_contract mc "
" LEFT JOIN members m ON m.id=mc.client_id "
" WHERE mc.status IN (1,2) AND mc.auto_remind < 3 "
" AND delivery_date < now() GROUP BY mc.member_id ,mc.auto_remind, contract_id ORDER BY mc.auto_remind DESC");
if (res != NULL && pgsql_num_rows(res) > 0) {
for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
if (f.empty()) continue;
CVars rec;
map_to_cvars(f, rec);
// note we send once in the loop
// we need the loop to mar items but wanted to send the email only once
if ( contract_id != rec["contract_id"].Long() ){
contract_id = rec["contract_id"].Long();
cron_email(WRB_CRONJOB_PASTDUE_ALERT, rec, out); // send the cron email
}
pgsql_exec("UPDATE members_jobs_contract SET auto_remind=auto_remind + 1,due_remind=now() "\
"WHERE member_id=%lu AND id = %lu ", rec["member_id"].Long(),rec["contract_id"].Long());
}
}
logfmt(logINFO, "/PastDueWarings()");
return 0;
}
long SignUpStats(CVars in, CVars &out) {
logfmt(logINFO, "SignUpStats()");
load_db_record(out, "SELECT count(id) AS today_signup FROM members WHERE added::date = now()::date");
load_db_record(out, "SELECT count(id) AS today_pending_signup FROM members_pending WHERE added::date = now()::date");
load_db_record(out, "SELECT TO_CHAR(NOW(), 'Day Mon dd, yyyy hh:mm AM') AS report_date");
load_db_record(out, "SELECT count(*) AS total_task_interest FROM members_offer_interest WHERE added::date = now()::date");
load_db_record(out, "SELECT count(id) AS total_active_public_jobs FROM members_jobs_offer WHERE status=1 AND public_view= 1 AND expire> now()");
load_db_record(out, "SELECT count(*) AS total_login FROM members_session WHERE created::date=now()::date");
load_db_record(out, "SELECT count(id) AS total_private_tasks FROM members_jobs_offer WHERE status=1 AND public_view= 0 AND expire> now()");
load_db_record(out, "SELECT count(*) AS count_mobile_login FROM mobile_login WHERE updated::date = now()::date");
in = out;
alert_email(WRB_CRONJOB_SIGNUP_ALERT, in, out);
logfmt(logINFO, "/SignUpStats()");
return 0;
}
long PaymentPending(CVars in, CVars &out) {
logfmt(logINFO, "PaymentPending()");
const PGresult *res = pgsql_query("SELECT count(id) AS preview_count,member_id FROM members_jobs_contract "\
" WHERE status = 4 AND pay_remind IS NULL AND delivery_date < now() +'-1 day(s)' "\
" GROUP BY member_id LIMIT 1");
if (res != NULL && pgsql_num_rows(res) > 0) {
for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
if (f.empty()) continue;
CVars rec;
map_to_cvars(f, rec);
job_email(JOBS_PAYMENT_DUE_MAIL, rec, out); // send the reminder email
pgsql_exec("UPDATE members_jobs_contract SET pay_remind=now() "\
"WHERE member_id=%lu AND status = 4 AND pay_remind IS NULL AND delivery_date < now() +'-1 day(s)' ", rec["member_id"].Long());
}
}
logfmt(logINFO, "/PaymentPending()");
return 0;
}
long DeadlineLineApprachClient(CVars in, CVars &out) // called
{
logfmt(logINFO, "DeadlineLineApprachClient()");
// First Reminder
const PGresult *res = pgsql_query("SELECT j.client_id,j.id AS contract_id FROM members_jobs_contract j "\
"WHERE j.due_remind IS NULL AND j.status IN (1,2) "\
"AND j.delivery_date > now() AND j.delivery_date < now() + '1 day' "\
"AND j.client_id IS NOT NULL AND j.client_id > 0 limit %lu", in["one_limit"].Long());
if (res != NULL && pgsql_num_rows(res) > 0) {
for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
if (f.empty()) continue;
CVars rec;
map_to_cvars(f, rec);
job_email(JOBS_DUE_APPROACH_MAIL, rec, out); // send the reminder email
pgsql_exec("UPDATE members_jobs_contract SET due_remind=now() WHERE id=%lu", rec["contract_id"].Long());
}
}
const PGresult *res2 = pgsql_query("SELECT j.client_id,j.id AS contract_id FROM members_jobs_contract j "\
"WHERE now() > j.due_remind + '6 hours' AND j.status IN (1,2) "\
"AND j.delivery_date > now() AND j.delivery_date < now() + '1 day' "\
"AND j.client_id IS NOT NULL AND j.client_id > 0 limit %lu", in["one_limit"].Long());
if (res2 != NULL && pgsql_num_rows(res2) > 0) {
for (int i = 0, n = pgsql_num_rows(res2); i < n; i++) {
map<const char*, const char*>f = pgsql_fetch_assoc(res2, i);
if (f.empty()) continue;
CVars rec;
map_to_cvars(f, rec);
job_email(JOBS_DUE_APPROACH_MAIL, rec, out); // send the reminder email
pgsql_exec("UPDATE members_jobs_contract SET due_remind=now() WHERE id=%lu", rec["contract_id"].Long());
}
}
logfmt(logINFO, "/DeadlineLineApprachClient()");
return 0;
}
long SignUpPendingReminder(CVars in, CVars &out) {
}