2020-09-03 18:28:29 +03:00
|
|
|
import HttpError from '@wasp/core/HttpError.js'
|
2020-10-19 15:45:54 +03:00
|
|
|
import { createNewUser } from '@wasp/core/auth.js'
|
|
|
|
|
|
|
|
export const signUp = async (args, context) => {
|
|
|
|
await createNewUser({ email: args.email, password: args.password })
|
|
|
|
}
|
2020-09-19 17:11:09 +03:00
|
|
|
|
|
|
|
export const createTask = async (task, context) => {
|
2020-10-19 15:45:54 +03:00
|
|
|
if (!context.user) {
|
|
|
|
throw new HttpError(403)
|
|
|
|
}
|
|
|
|
|
2020-09-30 22:16:41 +03:00
|
|
|
const Task = context.entities.Task
|
|
|
|
return Task.create({
|
2020-09-19 17:11:09 +03:00
|
|
|
data: {
|
2020-10-19 21:32:34 +03:00
|
|
|
description: task.description,
|
|
|
|
user: {
|
|
|
|
connect: { id: context.user.id }
|
|
|
|
}
|
2020-09-19 17:11:09 +03:00
|
|
|
}
|
|
|
|
})
|
2020-08-31 15:41:49 +03:00
|
|
|
}
|
2020-09-29 11:44:27 +03:00
|
|
|
|
2020-09-30 22:16:41 +03:00
|
|
|
export const updateTaskIsDone = async ({ taskId, newIsDoneVal }, context) => {
|
2020-10-19 15:45:54 +03:00
|
|
|
if (!context.user) {
|
|
|
|
throw new HttpError(403)
|
|
|
|
}
|
|
|
|
|
2020-09-30 22:16:41 +03:00
|
|
|
const Task = context.entities.Task
|
2020-10-19 21:32:34 +03:00
|
|
|
return Task.updateMany({
|
|
|
|
where: { id: taskId, user: { id: context.user.id } },
|
2020-09-29 11:44:27 +03:00
|
|
|
data: { isDone: newIsDoneVal }
|
|
|
|
})
|
|
|
|
}
|
2020-09-30 11:08:20 +03:00
|
|
|
|
2020-09-30 22:16:41 +03:00
|
|
|
export const deleteCompletedTasks = async (args, context) => {
|
2020-10-19 15:45:54 +03:00
|
|
|
if (!context.user) {
|
|
|
|
throw new HttpError(403)
|
|
|
|
}
|
|
|
|
|
2020-09-30 22:16:41 +03:00
|
|
|
const Task = context.entities.Task
|
|
|
|
await Task.deleteMany({
|
2020-10-19 21:32:34 +03:00
|
|
|
where: { isDone: true, user: { id: context.user.id } }
|
2020-09-30 11:08:20 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-30 22:16:41 +03:00
|
|
|
export const toggleAllTasks = async (args, context) => {
|
2020-10-19 15:45:54 +03:00
|
|
|
if (!context.user) {
|
|
|
|
throw new HttpError(403)
|
|
|
|
}
|
|
|
|
|
2020-10-19 21:32:34 +03:00
|
|
|
const whereIsDone = isDone => ({ isDone, user: { id: context.user.id } })
|
2020-09-30 22:16:41 +03:00
|
|
|
const Task = context.entities.Task
|
2020-10-19 21:32:34 +03:00
|
|
|
const notDoneTasksCount = await Task.count({ where: whereIsDone(false) })
|
2020-09-30 11:08:20 +03:00
|
|
|
|
|
|
|
if (notDoneTasksCount > 0) {
|
2020-10-19 21:32:34 +03:00
|
|
|
await Task.updateMany({ where: whereIsDone(false), data: { isDone: true } })
|
2020-09-30 11:08:20 +03:00
|
|
|
} else {
|
2020-10-19 21:32:34 +03:00
|
|
|
await Task.updateMany({ where: whereIsDone(true), data: { isDone: false } })
|
2020-09-30 11:08:20 +03:00
|
|
|
}
|
|
|
|
}
|