29 lines
1.1 KiB
JavaScript
29 lines
1.1 KiB
JavaScript
function stableStringify(value) {
|
|
if (value === null || typeof value !== 'object') return JSON.stringify(value);
|
|
if (Array.isArray(value)) return `[${value.map(stableStringify).join(',')}]`;
|
|
const keys = Object.keys(value).sort();
|
|
return `{${keys.map((k) => `${JSON.stringify(k)}:${stableStringify(value[k])}`).join(',')}}`;
|
|
}
|
|
|
|
function djb2Hash(str) {
|
|
let hash = 5381;
|
|
for (let i = 0; i < str.length; i += 1) {
|
|
hash = ((hash << 5) + hash + str.charCodeAt(i)) >>> 0;
|
|
}
|
|
return hash.toString(16).padStart(8, '0');
|
|
}
|
|
|
|
const FINGERPRINT_FIELDS = ['status', 'tecnico_atribuido', 'categoria', 'entidade'];
|
|
|
|
function buildContextFingerprint(ticket) {
|
|
const followups = (ticket.followups || []).map((f) => `${f.id}:${f.date}`).sort();
|
|
const tasks = (ticket.tasks || []).map((t) => `${t.id}:${t.status}:${t.date}`).sort();
|
|
const picked = {};
|
|
for (const field of FINGERPRINT_FIELDS) picked[field] = ticket[field];
|
|
picked.followups = followups;
|
|
picked.tasks = tasks;
|
|
return djb2Hash(stableStringify(picked));
|
|
}
|
|
|
|
module.exports = { stableStringify, djb2Hash, buildContextFingerprint };
|