From b5ba7997e36408c09e3528b76fb27d045a746981 Mon Sep 17 00:00:00 2001 From: Moises Marquez Date: Mon, 21 Mar 2022 19:51:19 +0100 Subject: [PATCH] Migration of todos to have completion status --- backend/daos/todosDao.js | 13 +++++++++++ backend/migrate-todos.js | 24 ++++++++++++++++++++ backend/services/todosService.js | 39 +++++++++++++++++++++++++++++++- backend/utils/models/todos.js | 8 +++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 backend/migrate-todos.js create mode 100644 backend/utils/models/todos.js diff --git a/backend/daos/todosDao.js b/backend/daos/todosDao.js index 12343994..0a2757a2 100644 --- a/backend/daos/todosDao.js +++ b/backend/daos/todosDao.js @@ -51,6 +51,19 @@ class TodosDao { throw new IoError('An error occurred when fetching the todos lists', {cause: err}); } } + + async getAllToDosLists() { + const dbConnect = mongoDbClient.getDb(); + + try { + return await this.mongoDbCollection(dbConnect) + .find({}) + .toArray(); + } catch (err) { + console.error(err.stack); + throw new IoError('An error occurred when fetching the todos lists', {cause: err}); + } + } } module.exports = TodosDao; \ No newline at end of file diff --git a/backend/migrate-todos.js b/backend/migrate-todos.js new file mode 100644 index 00000000..8439743b --- /dev/null +++ b/backend/migrate-todos.js @@ -0,0 +1,24 @@ +const mongoDbClient = require("./database/mongoDbClient"); +const ToDosService = require("./services/todosService"); + +mongoDbClient.connectToServer(async function (err) { + if (err) { + console.error(err); + process.exit(); + } + + const ToDosServiceInstance = new ToDosService(); + + // Naive migration of ToDos + await ToDosServiceInstance.migrateToDos(); + + const allNewToDosLists = await ToDosServiceInstance.getAllToDosLists(); + + const successfulMigration = allNewToDosLists.flatMap(todoList => todoList.todos).every(toDo => typeof toDo.task === 'string'); + + if (successfulMigration) { + console.log('successful migration'); + } else { + console.log('something went wrong with the migration migration'); + } +}); \ No newline at end of file diff --git a/backend/services/todosService.js b/backend/services/todosService.js index 7568ae7f..f5f224bc 100644 --- a/backend/services/todosService.js +++ b/backend/services/todosService.js @@ -1,5 +1,6 @@ const TodosDao = require('../daos/todosDao'); const ULID = require("ulid"); +const {todoStatus} = require("../utils/models/todos"); class TodosService { constructor () { @@ -19,10 +20,46 @@ class TodosService { } updateToDosList(todoListId, newToDoListTitle, newToDoListToDos) { - const nonEmptyToDos = newToDoListToDos.filter(todo => todo.length > 0); + const nonEmptyToDos = newToDoListToDos.filter(todo => todo.task.length > 0); return this.ToDosDao.updateToDosList(todoListId, newToDoListTitle, nonEmptyToDos); } + + getAllToDosLists() { + return this.ToDosDao.getAllToDosLists(); + } + + // Naive migration of data + async migrateToDos() { + const oldTodosLists = await this.getAllToDosLists(); + + console.log('oldTodosLists', oldTodosLists); + + const newToDosLists = oldTodosLists.map(oldToDoList => ({ + ...oldToDoList, + todos: oldToDoList.todos.map(oldToDo => ({ + task: oldToDo, + status: todoStatus.Pending + })) + })); + + console.log('newToDosLists', newToDosLists); + + const futureMigratedToDosLists = newToDosLists.map(toDoList => { + return this.updateToDosList(toDoList.id, toDoList.title, toDoList.todos); + }); + + try { + return await Promise.all(futureMigratedToDosLists); + } catch { + console.log('An error occurred during the migration'); + const futureOldToDosLists = oldTodosLists.map(toDoList => { + return this.updateToDosList(toDoList.id, toDoList.title, toDoList.todos); + }); + + return await Promise.all(futureOldToDosLists); + } + } } module.exports = TodosService; \ No newline at end of file diff --git a/backend/utils/models/todos.js b/backend/utils/models/todos.js new file mode 100644 index 00000000..fca9b76d --- /dev/null +++ b/backend/utils/models/todos.js @@ -0,0 +1,8 @@ +const ToDoStatus = { + Completed: 'Completed', + Pending: 'Pending' +}; + +module.exports = { + todoStatus: ToDoStatus +} \ No newline at end of file