import { Response } from "express"; import { logger } from "../app"; import { LivebeatRequest } from "../lib/request"; import { Beat } from "../models/beat/beat.model."; import { Phone } from "../models/phone/phone.model"; export async function GetPhone(req: LivebeatRequest, res: Response) { const phoneId: String = req.params['id']; // If none id provided, return all. if (phoneId === undefined) { const phone = await Phone.find({ user: req.user?._id }); res.status(200).send(phone); return; } // Check database for phone const phone = await Phone.findOne({ androidId: phoneId, user: req.user?._id }); if (phone === undefined) { res.status(404).send(); return; } // Get last beat const lastBeat = await Beat.findOne({ phone: phone?._id }).sort({ createdAt: -1 }); res.status(200).send({ phone, lastBeat }); } export async function PostPhone(req: LivebeatRequest, res: Response) { const androidId: String = req.body.androidId; const modelName: String = req.body.modelName; const displayName: String = req.body.displayName; const operatingSystem: String = req.body.operatingSystem; const architecture: String = req.body.architecture; if (androidId === undefined || modelName === undefined || displayName === undefined || operatingSystem === undefined || architecture === undefined) { logger.debug("Request to /phone failed because of missing parameters."); res.status(400).send(); return; } // Check if phone already exists const phone = await Phone.findOne({ androidId, user: req.user?._id }); if (phone !== null) { logger.debug("Request to /phone failed because phone already exists."); res.status(409).send(); return; } // Create phone await Phone.create({ androidId, displayName, modelName, operatingSystem, architecture, user: req.user?._id }); logger.info(`New device (${displayName}) registered for ${req.user?.name}.`) res.status(200).send(); }