Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions backend/daos/todosDao.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
24 changes: 24 additions & 0 deletions backend/migrate-todos.js
Original file line number Diff line number Diff line change
@@ -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');
}
});
39 changes: 38 additions & 1 deletion backend/services/todosService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const TodosDao = require('../daos/todosDao');
const ULID = require("ulid");
const {todoStatus} = require("../utils/models/todos");

class TodosService {
constructor () {
Expand All @@ -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;
8 changes: 8 additions & 0 deletions backend/utils/models/todos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const ToDoStatus = {
Completed: 'Completed',
Pending: 'Pending'
};

module.exports = {
todoStatus: ToDoStatus
}