Untitled
unknown
javascript
2 years ago
1.7 kB
8
Indexable
async function getSummaryInsightMilestone(filters, property) {
const milestone = {
property_name: property.attributes.name,
target: 0,
percentage: 0,
vary: 'faster',
previous_target: 0,
};
const { type } = filters;
const milestoneHistories = (
await MilestoneHistory.query((qb) => {
qb.where(
`${Constants.MILESTONE_HISTORIES_TABLE}.property_id`,
property.id,
);
qb.where(`${Constants.MILESTONE_HISTORIES_TABLE}.type`, type);
qb.whereRaw(
`${Constants.MILESTONE_HISTORIES_TABLE}.target = ${Constants.MILESTONE_HISTORIES_TABLE}.value`,
);
qb.orderBy(`${Constants.MILESTONE_HISTORIES_TABLE}.id`, 'desc');
qb.limit(2);
}).fetchAll()
)
.toJSON()
.map((item) => {
return {
...item,
date_duration: moment(item.updated_at)
.utc()
.diff(moment(item.created_at).utc(), 'days'),
};
});
if (milestoneHistories.length > 0) {
milestone.target =
type === 'dwell_time'
? convertSeconds(milestoneHistories[0].target, 'minutes')
: nFormatter(milestoneHistories[0].target, 1);
}
if (milestoneHistories.length > 1) {
const newDateDuration = milestoneHistories[0].date_duration;
const prevDateDuration = milestoneHistories[1].date_duration;
milestone.previous_target =
type === 'dwell_time'
? convertSeconds(milestoneHistories[1].target, 'minutes')
: nFormatter(milestoneHistories[1].target);
milestone.percentage = calculatePercentage(
newDateDuration,
prevDateDuration,
);
milestone.vary = getVary(prevDateDuration, newDateDuration, [
'faster',
'slower',
]);
}
return milestone;
}Editor is loading...
Leave a Comment