Fix analytics date fallback and RFV scoring
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m7s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m7s
This commit is contained in:
44
backend/sql/orderDateSql.js
Normal file
44
backend/sql/orderDateSql.js
Normal file
@@ -0,0 +1,44 @@
|
||||
const ISO_ORDER_YEAR_SQL = "substring(data_pedido from '^(\\d{4})[-/]')::int";
|
||||
const ISO_ORDER_MONTH_SQL = "substring(data_pedido from '^\\d{4}[-/](\\d{1,2})[-/]')::int";
|
||||
const ISO_ORDER_DAY_SQL = "substring(data_pedido from '^\\d{4}[-/]\\d{1,2}[-/](\\d{1,2})')::int";
|
||||
const BR_ORDER_DAY_SQL = "substring(data_pedido from '^(\\d{1,2})[-/]')::int";
|
||||
const BR_ORDER_MONTH_SQL = "substring(data_pedido from '^\\d{1,2}[-/](\\d{1,2})[-/]')::int";
|
||||
const BR_ORDER_YEAR_SQL = "substring(data_pedido from '^\\d{1,2}[-/]\\d{1,2}[-/](\\d{4})')::int";
|
||||
|
||||
const buildSafeDateSql = ({ yearSql, monthSql, daySql }) => {
|
||||
const candidateDateSql = `(make_date(${yearSql}, ${monthSql}, 1) + ((${daySql} - 1) * INTERVAL '1 day'))::date`;
|
||||
|
||||
return `
|
||||
CASE
|
||||
WHEN ${yearSql} BETWEEN 1 AND 9999
|
||||
AND ${monthSql} BETWEEN 1 AND 12
|
||||
AND ${daySql} BETWEEN 1 AND 31
|
||||
AND EXTRACT(MONTH FROM ${candidateDateSql})::int = ${monthSql}
|
||||
THEN ${candidateDateSql}
|
||||
ELSE NULL
|
||||
END
|
||||
`;
|
||||
};
|
||||
|
||||
const ORDER_DATE_SQL = `
|
||||
COALESCE(
|
||||
data_pedido_date,
|
||||
CASE
|
||||
WHEN data_pedido ~ '^\\d{4}[-/]\\d{1,2}[-/]\\d{1,2}' THEN ${buildSafeDateSql({
|
||||
yearSql: ISO_ORDER_YEAR_SQL,
|
||||
monthSql: ISO_ORDER_MONTH_SQL,
|
||||
daySql: ISO_ORDER_DAY_SQL
|
||||
})}
|
||||
WHEN data_pedido ~ '^\\d{1,2}[-/]\\d{1,2}[-/]\\d{4}' THEN ${buildSafeDateSql({
|
||||
yearSql: BR_ORDER_YEAR_SQL,
|
||||
monthSql: BR_ORDER_MONTH_SQL,
|
||||
daySql: BR_ORDER_DAY_SQL
|
||||
})}
|
||||
ELSE NULL
|
||||
END
|
||||
)
|
||||
`;
|
||||
|
||||
module.exports = {
|
||||
ORDER_DATE_SQL
|
||||
};
|
||||
Reference in New Issue
Block a user