shellphone.app/app/public-area/queries/get-metrics.ts

26 lines
580 B
TypeScript
Raw Normal View History

2021-08-28 20:17:01 +00:00
import { resolver } from "blitz";
2021-08-29 13:10:25 +00:00
2021-08-28 20:17:01 +00:00
import db from "../../../db";
export default resolver.pipe(async () => {
2021-08-29 13:10:25 +00:00
const [phoneNumbers, smsExchanged, allPhoneCalls] = await Promise.all([
db.phoneNumber.count(),
db.message.count(),
db.phoneCall.findMany(),
]);
const secondsCalled = allPhoneCalls.reduce<number>((seconds, phoneCall) => {
2021-08-28 20:17:01 +00:00
if (!phoneCall.duration) {
2021-08-29 13:10:25 +00:00
return seconds;
2021-08-28 20:17:01 +00:00
}
2021-08-29 13:10:25 +00:00
return seconds + Number.parseInt(phoneCall.duration);
2021-08-28 20:17:01 +00:00
}, 0);
const minutesCalled = Math.round(secondsCalled / 60);
return {
phoneNumbers,
smsExchanged,
minutesCalled,
};
});