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');
|
2020-04-30 02:34:14 +00:00
|
|
|
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');
|
2020-05-14 02:36:24 +00:00
|
|
|
const {v4: uuidv4} = require('uuid');
|
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);
|
2020-04-30 02:34:14 +00:00
|
|
|
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 {
|
2020-04-30 02:34:14 +00:00
|
|
|
...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);
|
2020-05-19 19:43:08 +00:00
|
|
|
response.send({code: 400, message: validation.messages()});
|
2020-03-17 22:16:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const body = request.body;
|
|
|
|
if (body.avatar) {
|
|
|
|
const file = await FileUtils.saveBase64File(body.avatar);
|
|
|
|
body.avatar = `/u/images/${file.fileName}`;
|
|
|
|
} else {
|
2020-05-19 19:43:08 +00:00
|
|
|
body.avatar = `/images/default-child-avatar.png`;
|
2020-03-17 22:16:34 +00:00
|
|
|
}
|
|
|
|
const child = await Child.create(body);
|
|
|
|
const link = await Link.create(
|
|
|
|
{user_id: auth.user.id, child_id: child.id, is_parent: true});
|
2020-05-19 19:43:08 +00:00
|
|
|
response.status(201);
|
|
|
|
return {code: 201, child};
|
2020-03-17 22:16:34 +00:00
|
|
|
}
|
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',
|
2020-04-28 03:04:09 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2020-05-23 21:32:25 +00:00
|
|
|
async updateUser({request, auth, response}) {
|
|
|
|
const user = auth.user;
|
|
|
|
const {name, email, profile_cover, avatar} = request.body;
|
|
|
|
// TODO: Validation
|
|
|
|
user.name = name || user.name;
|
|
|
|
user.email = email || user.email;
|
|
|
|
if (profile_cover) {
|
|
|
|
const file = await FileUtils.saveBase64File(profile_cover);
|
|
|
|
user.profile_cover = `/u/images/${file.fileName}`;
|
|
|
|
}
|
|
|
|
if (avatar) {
|
|
|
|
const file = await FileUtils.saveBase64File(avatar);
|
|
|
|
user.avatar = `/u/images/${file.fileName}`;
|
|
|
|
}
|
|
|
|
await user.save();
|
|
|
|
return {code: 0, data: {user}};
|
|
|
|
};
|
|
|
|
|
2020-05-21 01:18:56 +00:00
|
|
|
async updateChild({request, auth, response}) {
|
|
|
|
const childId = request.params.id;
|
|
|
|
const userId = auth.user.id;
|
|
|
|
const {name, dob, profile_cover, avatar} = request.body;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
// TODO: Add validation;
|
|
|
|
const child = await Child.find(childId);
|
|
|
|
child.dob = dob || child.dob;
|
|
|
|
if (profile_cover) {
|
|
|
|
const file = await FileUtils.saveBase64File(profile_cover);
|
|
|
|
child.profile_cover = `/u/images/${file.fileName}`;
|
|
|
|
}
|
|
|
|
if (avatar) {
|
2020-05-21 03:22:40 +00:00
|
|
|
const file = await FileUtils.saveBase64File(avatar);
|
2020-05-21 01:18:56 +00:00
|
|
|
child.avatar = `/u/images/${file.fileName}`;
|
|
|
|
}
|
|
|
|
await child.save();
|
|
|
|
return {code: 0, data: {child}};
|
|
|
|
}
|
|
|
|
|
2020-04-12 14:25:42 +00:00
|
|
|
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-05-24 19:59:44 +00:00
|
|
|
|
|
|
|
async createBook({request, response, auth}) {
|
|
|
|
// TODO: Validate input!
|
|
|
|
const user = auth.user;
|
|
|
|
const bookPayload = request.body;
|
|
|
|
// console.log('BookPages')
|
|
|
|
const bookHash = uuidv4();
|
|
|
|
const bookDrivePromises = [];
|
|
|
|
const bookRelativePath = `uploads/${bookHash}`;
|
|
|
|
const bookAbsolutePath = `books/${bookRelativePath}`;
|
|
|
|
for (let i = 0; i < bookPayload.pages.length; i++) {
|
|
|
|
const filePayload = bookPayload.pages[i];
|
|
|
|
bookDrivePromises.push(FileUtils.saveBase64File(
|
|
|
|
filePayload, `${bookAbsolutePath}/${i + 1}.jpg`));
|
|
|
|
};
|
|
|
|
await Promise.all(bookDrivePromises);
|
|
|
|
const book = await Book.create({
|
|
|
|
user_id: user.id,
|
|
|
|
title: bookPayload.title,
|
|
|
|
pages: bookPayload.pages.length,
|
|
|
|
book_folder: bookRelativePath,
|
|
|
|
ltr: bookPayload.ltr
|
|
|
|
});
|
|
|
|
return {code: 0, data: book};
|
|
|
|
}
|
2020-03-17 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ClientApiController
|