50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
import { Response } from "express";
|
|
import { LivebeatRequest } from "../lib/request";
|
|
import { IBeat } from "../models/beat/beat.interface";
|
|
import { Beat } from "../models/beat/beat.model.";
|
|
import { Phone } from "../models/phone/phone.model";
|
|
|
|
export async function GetBeatStats(req: LivebeatRequest, res: Response) {
|
|
const phones = await Phone.find({ user: req.user?._id }).exec();
|
|
const perPhone: any = {};
|
|
let totalBeats = 0;
|
|
|
|
if (phones[0] == undefined) return;
|
|
|
|
const phone = phones[0];
|
|
|
|
for (let i = 0; i < phones.length; i++) {
|
|
const beatCount = await Beat.countDocuments({ [phone.id]: phone.id });
|
|
perPhone[phones[i]._id] = {};
|
|
perPhone[phones[i]._id] = beatCount;
|
|
totalBeats += beatCount;
|
|
}
|
|
|
|
res.status(200).send({ totalBeats, perPhone });
|
|
}
|
|
|
|
export async function GetBeat(req: LivebeatRequest, res: Response) {
|
|
const from: number = Number(req.query.from);
|
|
const to: number = Number(req.query.to);
|
|
const limit: number = Number(req.query.limit || 10000);
|
|
const sort: number = Number(req.query.sort || 1); // Either -1 or 1
|
|
const phoneId = req.query.phoneId;
|
|
|
|
// Grab default phone if non was provided.
|
|
const phone = req.query.phone === undefined ? await Phone.findOne({ user: req.user?._id }) : await Phone.findOne({ _id: phoneId, user: req.user?._id });
|
|
let beats: IBeat[] = []
|
|
|
|
if (phone !== null) {
|
|
beats = await Beat.find(
|
|
{
|
|
phone: phone._id,
|
|
createdAt: {
|
|
$gte: new Date((from | 0) * 1000),
|
|
$lte: new Date((to | Date.now() /1000) * 1000)
|
|
}
|
|
}).sort({ _id: sort }).limit(limit);
|
|
res.status(200).send(beats);
|
|
} else {
|
|
res.status(404).send({ message: 'Phone not found' });
|
|
}
|
|
} |