This repository has been archived on 2024-12-15. You can view files and clone it, but cannot push or open issues or pull requests.
seepur/app/Controllers/Http/ClientApiController.js

200 lines
6.1 KiB
JavaScript
Raw Normal View History

2020-03-17 22:16:34 +00:00
'use strict'
const {validate, rule} = use('Validator');
const User = use('App/Models/User');
const Child = use('App/Models/Child')
const Link = use('App/Models/Link');
2020-04-12 14:25:42 +00:00
const Call = use('App/Models/Call');
const Book = use('App/Models/Book');
2020-04-12 14:25:42 +00:00
2020-03-17 22:16:34 +00:00
const FileUtils = use('App/Utils/FileUtils');
2020-04-12 14:25:42 +00:00
const UserChildUtils = use('App/Utils/UserChildUtils');
const uuidv4 = require('uuid').v4;
2020-03-17 22:16:34 +00:00
class ClientApiController {
async getUser({auth}) {
const user = auth.user.toJSON();
2020-04-12 14:25:42 +00:00
const connections = await UserChildUtils.getUserConnections(user.id);
const booksResponse = await Book.query()
.where({user_id: null})
.orWhere({user_id: user.id})
.fetch();
let books = [];
if (booksResponse.rows.length) books = booksResponse.rows;
2020-03-17 22:16:34 +00:00
return {
...user, connections: {...connections}, books
2020-03-17 22:16:34 +00:00
}
}
async createChild({auth, request, response}) {
const rules = {
name: 'required|string',
dob: 'required|date',
avatar: [rule('regex', /^(data:image\/\w+;base64).+/)]
};
const validation = await validate(request.all(), rules);
if (validation.fails()) {
response.status(400);
response.send(validation.messages());
return false;
}
const body = request.body;
if (body.avatar) {
const file = await FileUtils.saveBase64File(body.avatar);
console.log(file);
body.avatar = `/u/images/${file.fileName}`;
} else {
body.avatar =
`https://api.adorable.io/avatars/285/${body.name.trim()}.png`;
}
const child = await Child.create(body);
const link = await Link.create(
{user_id: auth.user.id, child_id: child.id, is_parent: true});
return child;
}
2020-04-12 14:25:42 +00:00
2020-04-14 00:56:04 +00:00
async getBooks() {}
async getCallBooks() {}
2020-04-12 14:25:42 +00:00
async createCall({auth, request, response}) {
try {
const user = auth.user;
const rules = {
connection_id: 'number|required',
child_id: 'number|required',
};
const validation = await validate(request.all(), rules);
if (validation.fails()) {
response.status(400);
response.send(validation.messages());
return false;
}
const body = request.body;
if (!(await UserChildUtils.isParentOf(user.id, body.child_id))) {
response.status(403);
response.send({code: 403, message: 'Unauthorized'});
return false;
}
if (!(await UserChildUtils.isUserConnectedToChild(
body.connection_id, body.child_id))) {
response.status(403);
response.send({code: 403, message: 'Unauthorized'});
return false;
}
const call = await Call.create({
state: 'NEW',
parent_id: user.id,
guest_id: body.connection_id,
2020-04-12 14:25:42 +00:00
child_id: body.child_id
});
return {
code: 0, data: call
}
} catch (error) {
console.error(error);
return error;
}
}
async getChild({auth, request, response}) {
const userId = auth.user.id;
const childId = request.params.id;
console.log(`${userId} -> ${childId}`);
const hasPermission =
await UserChildUtils.isUserConnectedToChild(userId, childId);
if (!hasPermission) {
response.status(403);
response.send(
{code: 403, message: `You have no permission to connect with child`});
return false;
}
const child = await Child.find(childId);
const parents = await UserChildUtils.getChildParents(childId);
const connections = await UserChildUtils.getChildConnections(childId);
return {
code: 0, data: {...child.toJSON(), parents, connections}
}
}
async createConnection({request, auth, response}) {
try {
const user = auth.user;
const rules = {
email: 'string|email|required',
is_parent: 'boolean|required',
child_id: 'number|required'
};
const validation = await validate(request.all(), rules);
if (validation.fails()) {
response.status(400);
response.send(validation.messages());
return false;
}
const body = request.body;
if (!await UserChildUtils.isParentOf(user.id, body.child_id)) {
response.status(403);
response.send({
code: 403,
message: `You have no permission to add connection to child`
});
return false;
}
const usersWithEmail =
(await User.query().where({email: body.email}).fetch()).rows;
if (!usersWithEmail.length) {
return {code: 404, message: 'No user with that Email...'};
}
const targetUser = usersWithEmail[0];
if (await UserChildUtils.isUserConnectedToChild(
targetUser.id, body.child_id)) {
return {code: 409, message: 'User already connected'};
}
return {
code: 0,
data: await UserChildUtils.addConnection(
body.child_id, targetUser.id, body.is_parent)
};
} catch (error) {
console.error(error);
return error;
}
//
}
async setChildProfileCover({request, auth, response}) {
try {
const rules = {
profile_cover: [rule('regex', /^(data:image\/\w+;base64).+/)]
};
const validation = await validate(request.all(), rules);
if (validation.fails()) {
response.status(400);
response.send(validation.messages());
return false;
}
const body = request.body;
const userId = auth.user.id;
const childId = request.params.id;
const isParent = await UserChildUtils.isParentOf(userId, childId);
if (!isParent) {
response.status(403);
response.send(
{code: 403, message: `You have no permission to edit this child`});
return false;
}
const child = await Child.find(childId);
const file = await FileUtils.saveBase64File(body.profile_cover);
console.log(file);
child.profile_cover = `/u/images/${file.fileName}`;
await child.save();
return child.profile_cover;
} catch (error) {
console.error(error);
return error;
}
}
2020-03-17 22:16:34 +00:00
}
module.exports = ClientApiController