Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update db log flow #286

Draft
wants to merge 2 commits into
base: feat/sync
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
binary
.DS_Store
xeno

# Logs
logs
Expand Down
8 changes: 7 additions & 1 deletion cli/lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const setStart = (args, options, logger) => {
return db
.updateDatabase(payload, db.knex)
.then(() => {
// Here we have to add a new edit log
spinnerSucceed(spinner, `You clocked in at: ${start}\n\n`).start();
helpers.shouldWorkUntil(start, config);
})
Expand Down Expand Up @@ -85,6 +86,7 @@ const setBreak = (args, options, logger) => {
.updateDatabase(payload, db.knex)
.catch(spinner.fail)
.then(() => {
// Here we have to add a new edit log
report();
});
};
Expand Down Expand Up @@ -180,6 +182,7 @@ const setEnd = (args, options, logger) => {
action: 'setEnd',
};
return db.updateDatabase(payload, db.knex).then(() => {
// Here we have to add a new edit log
report();
});
};
Expand Down Expand Up @@ -209,7 +212,10 @@ const addNote = (args, options, logger) => {
};
return db
.updateDatabase(payload, db.knex)
.then(() => report())
.then(() => {
// Here we have to add a new edit log
report()
})
.catch(spinner.fail)
.finally(() => {
spinnerSucceed(spinner, constants.TEXT.addNoteSuccess).start();
Expand Down
65 changes: 48 additions & 17 deletions cli/lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,27 @@ const updateDatabase = (options, knex) => {
}
} else {
// date doesn't exist, insert it
if (action === 'addNote')
throw 'Add the start time first'
return knex.insert({ date, start, end, breakDuration }).into('records');
}
})
.catch(spinner.fail);
.then((result) => {
// Database has been affected
// Here we have to add a new log
console.log('Db affected...');
const type = action === 'addNote' ? 'notes' : 'records'
return knex.select('*').from('records').where({ date }).then((result) => {
console.log('records checked for id >>', result);
return setLogData(knex, { action, type, dataId: result[0].id })
.then((result) => {
console.log('result', result);
})
})
// .catch((err) => { console.log(err); })
})
// .catch((err) => { console.log(err); })
// .catch(spinner.fail);
};

// gets data for a single day
Expand Down Expand Up @@ -184,18 +201,25 @@ const getRowsAfter = (knex, table, recordId) =>
.catch(spinner.fail);

// Create a log table
const createLogTable = (knex) =>
knex.schema.hasTable('logs').then((exists) => {
if (!exists) {
return knex.schema
.createTable('logs', (table) => {
table.increments('id');
table.string('type');
table.integer('lastLogId');
})
.catch((e) => spinner.fail(`Errors in createTable ${e}`));
}
});
const createLogTable = (knex) => {
console.log('Check if log Table exists...');
return knex.schema.hasTable('logs')
.then((exists) => {
if (!exists) {
return knex.schema
.createTable('logs', (table) => {
table.increments('id');
table.string('action');
table.string('type');
table.integer('dataId');
})
.catch((err) => { console.log('exists err', err) })

// .catch((e) => spinner.fail(`Errors in createTable ${e}`));
}
})
// .catch((err) => { console.log(err) })
}

const getLastLogData = (knex, type) =>
createLogTable(knex)
Expand All @@ -208,14 +232,21 @@ const getLastLogData = (knex, type) =>
.limit(1)
.catch((e) => spinner.fail(`Errors in fetch - ${e}`)),
)
.catch((e) => spinner.fail(`Errors in createLogTable - ${e}`));
.catch((e) => spinner.fail(`Errors in createTable - ${e}`));

const setLogData = (knex, data) =>
createLogTable(knex).then(() => knex.insert(data).into('logs'));
const setLogData = (knex, data) => {
console.log('Set log data...');
console.log(data);
return createLogTable(knex)
.then(() => {
console.log('Finalize Log...');
return knex.insert(data).into('logs')
})
.catch((e) => { console.log(e);})
}

module.exports = {
createTable,
createLogTable,
getDateReport,
updateDatabase,
getLastLogData,
Expand Down