Livebeat is now able to send, store and show beats
This commit is contained in:
63
backend/endpoints/phone.ts
Normal file
63
backend/endpoints/phone.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { Response } from "express";
|
||||
import { logger } from "../app";
|
||||
import { LivebeatRequest } from "../lib/request";
|
||||
import { Phone } from "../models/phone/phone.model";
|
||||
|
||||
export async function GetPhone(req: LivebeatRequest, res: Response) {
|
||||
const phoneId: String = req.params['id'];
|
||||
|
||||
if (phoneId === undefined) {
|
||||
res.status(400).send();
|
||||
return;
|
||||
}
|
||||
|
||||
// Check database for phone
|
||||
const phone = await Phone.findOne({ androidId: phoneId, user: req.user?._id });
|
||||
if (phone === undefined) {
|
||||
res.status(404).send();
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(200).send(phone);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
Reference in New Issue
Block a user