Skip to content

Commit 7baf0ee

Browse files
committed
chore: Cleanups.
1 parent 050e374 commit 7baf0ee

3 files changed

Lines changed: 70 additions & 79 deletions

File tree

js/info.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const entityAPIs = (model, protocol, baseUrl, fullDescription) => {
5252
delete: { method: "DELETE", url: pathToModel + "/{id}" },
5353
};
5454
} else {
55-
mi.apis = `${protocol}${baseUrl}?id=${model.id}`;
55+
mi.apis = `${protocol}${baseUrl}/?id=${model.id}`;
5656
}
5757
return mi;
5858
};

js/lov.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,41 +34,36 @@ const SQLlovOne = (f, search) => {
3434

3535
// - returns list of possible values for a field (usually for dropdown)
3636
// - sample url: http://localhost:2000/api/v1/todo/lov/category
37-
export const lovOne = async (req, res) => {
37+
export const lovOne = (req, res) => {
3838
logger.logReq("LOV ONE", req);
3939
const mid = req.params.entity,
4040
m = getModel(mid),
4141
fid = req.params.field,
4242
search = req.query.search;
43-
let f = m.fieldsH[fid];
4443

45-
if (m) {
46-
if (!f && fid === mid) {
47-
// -- if field id = entity id, then use the entity itself as the lov
48-
f = {
49-
id: "entity",
50-
lovColumn: m.fields[0].column,
51-
lovTable: m.table,
52-
};
53-
}
54-
if (f) {
55-
// const col = f.lovColumn || "name";
56-
let params = null;
57-
let sql = SQLlovOne(f, search);
58-
if (search) {
59-
params = [searchParam(search)];
60-
}
61-
runQuery(res, sql, params, false);
62-
} else {
63-
res.json(logger.errorMsg('Invalid field "' + fid + '".', "lovOne"));
64-
}
65-
} else {
66-
badRequest(res);
44+
if (!m) {
45+
return badRequest(res, `Model not found: "${mid}".`, 404);
46+
}
47+
48+
let f = m.fieldsH[fid];
49+
if (!f && fid === mid) {
50+
// if field id = entity id, use the entity itself as the lov
51+
f = {
52+
id: "entity",
53+
lovColumn: m.fields[0].column,
54+
lovTable: m.table,
55+
};
56+
}
57+
if (!f) {
58+
return badRequest(res, `Invalid field "${fid}".`, 400);
6759
}
60+
61+
const sql = SQLlovOne(f, search);
62+
const params = search ? [searchParam(search)] : [];
63+
runQuery(res, sql, params, false);
6864
};
6965

7066
export default {
71-
// - LOVs (for dropdowns)
7267
lovOne,
7368
SQLlovOne,
7469
};

js/stats.js

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import { getModel } from "./utils/model-manager.js";
1010
import dico, { fieldTypes as ft } from "./utils/dico.js";
1111
import { runQuery } from "./utils/query.js";
12-
import errors from "./utils/errors.js";
12+
import { badRequest } from "./utils/errors.js";
1313
import logger from "./utils/logger.js";
1414
import config from "../config.js";
1515

@@ -83,70 +83,66 @@ export function getStats(req, res) {
8383

8484
const mid = req.params.entity,
8585
m = getModel(mid);
86-
let sqlNulls = [];
86+
87+
if (!m) {
88+
return badRequest(res, `Model not found: "${mid}".`, 404);
89+
}
90+
if (m.noStats) {
91+
return badRequest(res, `noStats=true on model "${mid}".`, 400);
92+
}
8793

8894
const sqlFROM = " FROM " + m.schemaTable;
8995
const sqlNull = (f) =>
9096
`(SELECT COUNT(*)::integer AS "${f.id}_nulls" ${sqlFROM} WHERE "${f.column}" IS NULL)`;
9197
const sqlNullorEmpty = (f) =>
9298
`(SELECT COUNT(*)::integer AS "${f.id}_nulls" ${sqlFROM} WHERE "${f.column}" IS NULL OR "${f.column}"='')`;
9399

94-
if (m) {
95-
if (!m.noStats) {
96-
let sql = "SELECT count(*)::integer AS count";
100+
let sql = "SELECT count(*)::integer AS count";
101+
const sqlNulls = [];
97102

98-
m.fields.forEach(function (f) {
99-
if (!f.noStats) {
100-
if (dico.fieldIsNumeric(f)) {
101-
if (!dico.fieldIsDateOrTime(f)) {
102-
sql +=
103-
sqlAggregate("avg", f) +
104-
sqlAggregate("stddev", f) +
105-
sqlAggregate("variance", f);
106-
}
107-
if (f.type === ft.money || f.type === ft.int) {
108-
// TODO: should we? (it may be too big a number)
109-
sql += sqlAggregate("sum", f);
110-
}
111-
sql += sqlAggregate("min", f);
112-
sql += sqlAggregate("max", f);
113-
//}else if(f.type===ft.lov){
114-
// sql += ',(select id, count("'+f.column+'")::integer FROM '+m.schemaTable+' GROUP BY id LIMIT 3)'
115-
sqlNulls.push(sqlNull(f));
116-
} else {
117-
if (f.type === "lov" || f.type === "boolean") {
118-
sqlNulls.push(sqlNull(f));
119-
} else {
120-
sqlNulls.push(sqlNullorEmpty(f));
121-
}
122-
}
103+
m.fields.forEach((f) => {
104+
if (!f.noStats) {
105+
if (dico.fieldIsNumeric(f)) {
106+
if (!dico.fieldIsDateOrTime(f)) {
107+
sql +=
108+
sqlAggregate("avg", f) +
109+
sqlAggregate("stddev", f) +
110+
sqlAggregate("variance", f);
111+
}
112+
if (f.type === ft.money || f.type === ft.int) {
113+
sql += sqlAggregate("sum", f);
114+
}
115+
sql += sqlAggregate("min", f);
116+
sql += sqlAggregate("max", f);
117+
sqlNulls.push(sqlNull(f));
118+
} else {
119+
if (f.type === ft.lov || f.type === ft.bool) {
120+
sqlNulls.push(sqlNull(f));
121+
} else {
122+
sqlNulls.push(sqlNullorEmpty(f));
123123
}
124-
});
125-
if (config.wTimestamp) {
126-
// - last update
127-
sql += `, max(${config.updatedDateColumn}) AS u_date_max`;
128-
// - number of insert & updates this week
129-
sql +=
130-
`, (SELECT count(${m.pKey})::integer ${sqlFROM}` +
131-
" WHERE " +
132-
config.updatedDateColumn +
133-
" > NOW() - interval '7 days') AS u_date_week_count" +
134-
// - first insert
135-
`, min(${config.createdDateColumn}) AS c_date_min`;
136-
}
137-
if (config.wComments) {
138-
// - number of comments
139-
sql += ", sum(nb_comments::integer)::integer AS nb_comments";
140124
}
141-
sql += ", " + sqlNulls.join(", ");
142-
sql += sqlFROM;
143-
runQuery(res, sql, [], true, null, null, fnPrep(m.fields));
144-
} else {
145-
errors.badRequest(res, 'noStats=true on model "' + mid + '".', 401);
146125
}
147-
} else {
148-
errors.badRequest(res, 'Model not found: "' + mid + '".', 404);
126+
});
127+
128+
if (config.wTimestamp) {
129+
// - last update
130+
sql += `, max(${config.updatedDateColumn}) AS u_date_max`;
131+
// - number of insert & updates this week
132+
sql +=
133+
`, (SELECT count(${m.pKey})::integer ${sqlFROM}` +
134+
" WHERE " +
135+
config.updatedDateColumn +
136+
" > NOW() - interval '7 days') AS u_date_week_count" +
137+
// - first insert
138+
`, min(${config.createdDateColumn}) AS c_date_min`;
139+
}
140+
if (config.wComments) {
141+
sql += ", sum(nb_comments::integer)::integer AS nb_comments";
149142
}
143+
sql += ", " + sqlNulls.join(", ");
144+
sql += sqlFROM;
145+
runQuery(res, sql, [], true, null, null, fnPrep(m.fields));
150146
}
151147

152148
// --------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)