- Device can now become (in)active - Error alert makes sound - Alerts now execute function on click
72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
import { Response } from "express";
|
|
import { logger, rabbitmq } 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 no 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 === null) {
|
|
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) {
|
|
res.status(409).send({ message: "This phone already exists." });
|
|
return;
|
|
}
|
|
|
|
// Create phone
|
|
const newPhone = await Phone.create({
|
|
androidId,
|
|
displayName,
|
|
modelName,
|
|
operatingSystem,
|
|
architecture,
|
|
user: req.user?._id,
|
|
active: false
|
|
});
|
|
|
|
logger.info(`New device (${displayName}) registered for ${req.user?.name}.`);
|
|
rabbitmq.publish(req.user?.id, newPhone.toJSON(), 'phone_register')
|
|
|
|
res.status(200).send();
|
|
} |