242 lines
7.9 KiB
C++
242 lines
7.9 KiB
C++
#include "clog.h"
|
|
#include "cgi.h"
|
|
#include "input.h"
|
|
#include "wrenchboard_api.h"
|
|
#include "reco_engine.h" //error in file name
|
|
#include "email.h"
|
|
#include "safestring.h"
|
|
#include <string>
|
|
#include "pgsql.h"
|
|
#include "pgsql_wrapper.h"
|
|
#include <curl/curl.h>
|
|
#include "account.h"
|
|
#include "cards.h"
|
|
#include "twilo.h"
|
|
#include "mobile.h"
|
|
#include "jobs_manager.h"
|
|
#include "family_acc.h"
|
|
#include "common_tool.h"
|
|
|
|
long WrenchFamilyList(CVars in, CVars &out);
|
|
long WrenchFamilyAdd(CVars in, CVars &out);
|
|
long WrenchFamilyUpdate(CVars in, CVars &out);
|
|
long WrenchFamilyManage(CVars in, CVars &out);
|
|
|
|
long family_calls(CVars in, CVars &out){
|
|
logfmt(logINFO, "family_calls()");
|
|
out["result"] = "YES I GET TO BACK END";
|
|
long action = REQ_LONG(in, "action", 0, -1);
|
|
CVars x;
|
|
long ret = 0;
|
|
const char * loc = getenv("REMOTE_ADDR");
|
|
const PGresult *res;
|
|
logfmt(logINFO, "action = %ld", action);
|
|
|
|
switch (action) {
|
|
case WRENCHBOARD_FAMILY_LIST:
|
|
return WrenchFamilyList(in, out);
|
|
break;
|
|
|
|
case WRENCHBOARD_FAMILY_ADD:
|
|
return WrenchFamilyAdd(in, out);
|
|
break;
|
|
|
|
case WRENCHBOARD_FAMILY_UPDATE:
|
|
return WrenchFamilyUpdate(in, out);
|
|
break;
|
|
|
|
case WRENCHBOARD_FAMILY_MANAGE:
|
|
return WrenchFamilyManage(in, out);
|
|
break;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
long WrenchFamilyList(CVars in, CVars &out){
|
|
logfmt(logINFO, "ENTER CALL long WrenchFamilyList");
|
|
char vname[30];
|
|
long ret = PHP_API_BAD_PARAM;
|
|
const PGresult *res;
|
|
try{
|
|
long member_id = REQ_LONG(in, "member_id", 1, -1);
|
|
long offset = REQ_LONG(in, "offset", 1, -1);
|
|
long limit = REQ_LONG(in, "limit", 1, -1);
|
|
|
|
out["total_record"] = "0";
|
|
res = pgsql_query("SELECT id FROM members_family WHERE member_id = %lu AND status=1 ", in["member_id"].Long());
|
|
out["total_record"] = pgsql_num_rows(res);
|
|
|
|
res = pgsql_query(" SELECT * FROM members_family WHERE member_id = %lu AND status=1 "
|
|
" ORDER BY id DESC LIMIT %lu OFFSET %lu", in["member_id"].Long(), limit, offset);
|
|
|
|
if (res != NULL && pgsql_num_rows(res) > 0) {
|
|
out["total_record"] = pgsql_num_rows(res);
|
|
|
|
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);
|
|
|
|
snprintf(vname, sizeof (vname), "uid_%05d", i);
|
|
out[vname] = rec["uid"];
|
|
|
|
snprintf(vname, sizeof (vname), "firstname_%05d", i);
|
|
out[vname] = rec["firstname"];
|
|
|
|
snprintf(vname, sizeof (vname), "lastname_%05d", i);
|
|
out[vname] = rec["lastname"];
|
|
|
|
snprintf(vname, sizeof (vname), "added_%05d", i);
|
|
out[vname] = rec["added"];
|
|
|
|
snprintf(vname, sizeof (vname), "age_%05d", i);
|
|
out[vname] = rec["age"];
|
|
|
|
snprintf(vname, sizeof (vname), "last_login_%05d", i);
|
|
out[vname] = rec["last_login"];
|
|
|
|
snprintf(vname, sizeof (vname), "task_count_%05d", i);
|
|
out[vname] = rec["task_count"];
|
|
|
|
}
|
|
}
|
|
ret = PHP_API_OK;
|
|
out["status"] = "OK";
|
|
|
|
} catch (bad_parameter) {
|
|
logfmt(logINFO, "ERROR CALL long WrenchFamilyList");
|
|
}
|
|
|
|
logfmt(logINFO, "WrenchFamilyList()");
|
|
return ret;
|
|
}
|
|
|
|
long WrenchFamilyAdd(CVars in, CVars &out){
|
|
logfmt(logINFO, "WrenchFamilyAdd()");
|
|
long family_id = 0;
|
|
|
|
try {
|
|
REQ_LONG(in, "member_id", 1, -1);
|
|
REQ_LONG(in, "age", 1, -1);
|
|
REQ_STRING(in, "firstname", 2, 34, "(.*)");
|
|
REQ_STRING(in, "lastname", 2, 34, "(.*)");
|
|
CVars x;
|
|
x["member_id"] = in["member_id"];
|
|
x["member_id"].set_valid(true);
|
|
x["age"] = in["age"];
|
|
x["age"].set_valid(true);
|
|
x["firstname"] = in["firstname"];
|
|
x["firstname"].set_valid(true);
|
|
x["lastname"] = in["lastname"];
|
|
x["lastname"].set_valid(true);
|
|
x["status"] = "1";
|
|
x["status"].set_valid(true);
|
|
family_id = insert_db_record(DBS_VALID, "members_family", "members_family_id_seq", x);
|
|
if (family_id > 0) {
|
|
load_db_record(out, "SELECT * FROM members_family WHERE id =%lu",family_id);
|
|
// send notice here
|
|
}
|
|
out["status"] = "OK";
|
|
} catch (bad_parameter) {
|
|
logfmt(logINFO, "ERROR CALL long WrenchFamilyAdd(CVars in, CVars &out)");
|
|
}
|
|
logfmt(logINFO, "WrenchFamilyAdd()");
|
|
return family_id;
|
|
}
|
|
|
|
long WrenchFamilyUpdate(CVars in, CVars &out){
|
|
long ret = PHP_API_BAD_PARAM;
|
|
logfmt(logINFO, "WrenchFamilyUpdate()");
|
|
long family_id = 0;
|
|
try {
|
|
long member_id = REQ_LONG(in, "member_id", 1, -1);
|
|
REQ_STRING(in, "firstname", 2, 34, "(.*)");
|
|
REQ_STRING(in, "lastname", 2, 34, "(.*)");
|
|
REQ_STRING(in, "family_uid", 2, 50, "(.*)");
|
|
CVars x;
|
|
x["firstname"] = in["firstname"];
|
|
x["firstname"].set_valid(true);
|
|
x["lastname"] = in["lastname"];
|
|
x["lastname"].set_valid(true);
|
|
|
|
const PGresult *res = pgsql_query(" SELECT id AS family_id FROM members_family "
|
|
" WHERE member_id=%lu AND uid='%s' ", member_id,in["family_uid"].c_str());
|
|
if (res != NULL && pgsql_num_rows(res) > 0) {
|
|
map<const char*, const char*>f = pgsql_fetch_assoc(res, 0);
|
|
CVars rec;
|
|
map_to_cvars(f, rec);
|
|
update_db_record(DBS_VALID, "members_family", x, rec["family_id"].Long());
|
|
ret = PHP_API_OK;
|
|
}
|
|
out["status"] = "OK";
|
|
} catch (bad_parameter) {
|
|
logfmt(logINFO, "ERROR CALL long WrenchFamilyUpdate(CVars in, CVars &out)");
|
|
}
|
|
logfmt(logINFO, "WrenchFamilyUpdate()");
|
|
return ret;
|
|
}
|
|
|
|
long WrenchFamilyInitializeAccount(CVars in, CVars &out);
|
|
long WrenchFamilyManage(CVars in, CVars &out){
|
|
/*
|
|
wrenchboard=> select * from members_family;
|
|
id | member_id | firstname | lastname | age | active | status | added | updated
|
|
----+-----------+-----------+----------+-----+--------+--------+-------+---------
|
|
(0 rows)
|
|
*/
|
|
long ret = PHP_API_BAD_PARAM;
|
|
logfmt(logINFO, "WrenchFamilyManage()");
|
|
long family_id = 0;
|
|
try {
|
|
long member_id = REQ_LONG(in, "member_id", 1, -1);
|
|
REQ_STRING(in, "uid", 2, 45, "(.*)");
|
|
REQ_STRING(in, "family_uid", 2, 45, "(.*)");
|
|
|
|
const PGresult *res = pgsql_query(" SELECT id AS family_id,* FROM members_family "
|
|
" WHERE member_id=%lu AND uid='%s' ", member_id,in["family_uid"].c_str());
|
|
if (res != NULL && pgsql_num_rows(res) > 0) {
|
|
map<const char*, const char*>f = pgsql_fetch_assoc(res, 0);
|
|
CVars rec;
|
|
map_to_cvars(f, rec);
|
|
if( rec["username"]=="" || rec["pin"]==""){
|
|
logfmt(logINFO, "WrenchFamilyManage()- NEED TI INITIALIZE USERNAME PIN");
|
|
WrenchFamilyInitializeAccount(rec,out);
|
|
}
|
|
out = rec;
|
|
ret = PHP_API_OK;
|
|
}
|
|
out["status"] = "OK";
|
|
} catch (bad_parameter) {
|
|
logfmt(logINFO, "ERROR CALL long WrenchFamilyUpdate(CVars in, CVars &out)");
|
|
}
|
|
|
|
logfmt(logINFO, "/WrenchFamilyManage()");
|
|
return ret;
|
|
}
|
|
|
|
long WrenchFamilyInitializeAccount(CVars in, CVars &out){
|
|
logfmt(logINFO, "WrenchFamilyInitializeAccount()");
|
|
|
|
char username[15] = "";
|
|
SetFamilyUsername(in["family_id"].Long(), username, sizeof (username));
|
|
|
|
int r1;
|
|
srand(time(NULL));
|
|
r1 = abs(rand()*10000);
|
|
pgsql_exec("UPDATE members_family SET pin='%s' WHERE id=%lu", r1, in["family_id"].Long() );
|
|
|
|
load_db_record(out, "SELECT * FROM members_family WHERE id = %lu", in["family_id"].Long());
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
#define WRENCHBOARD_FAMILY_LIST 22010
|
|
#define WRENCHBOARD_FAMILY_ADD 22015
|
|
#define WRENCHBOARD_FAMILY_UPDATE 22020
|
|
#define WRENCHBOARD_FAMILY_MANAGE 22025
|
|
*/ |