Skip to content

Commit

Permalink
feat: soft delete project
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypatel1210 committed Nov 23, 2024
1 parent 6f68d3b commit e0d8a56
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
13 changes: 9 additions & 4 deletions server/src/projects.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
addRecord,
createCurriedQuery,
deleteRecord,
updateRecord,
} from './utils/firestore-db.js';
import verifyFirebaseToken from './middleware/verify-token.js';
import generateRandomID from './utils/generateRandomID.js';
Expand Down Expand Up @@ -55,11 +55,15 @@ function registerProjectRoutes(app) {
}
});

app.delete('/project/:projectId', verifyFirebaseToken, async (req, res) => {
const projectId = req.params.projectId;
app.post('/delete-project', verifyFirebaseToken, async (req, res) => {
const projectId = req.body?.projectId;

try {
await deleteRecord('projects', projectId);
await updateRecord('projects', projectId, {
status: 'DELETE',
deletedOn: new Date(),
});

res.status(200).send({
message: 'Project deleted successfully.',
});
Expand All @@ -85,6 +89,7 @@ function registerProjectRoutes(app) {
try {
const projects = await createCurriedQuery('projects')([
['uid', '==', uid],
['status', '!=', 'DELETE'],
])('created')('desc')();

res.status(200).send({
Expand Down
30 changes: 17 additions & 13 deletions server/src/utils/firestore-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@ async function readRecord(collectionName, docId) {
}
}

// async function updateRecord(collectionName, data, docId = null) {
// try {
// const docRef = docId
// ? db.collection(collectionName).doc(docId)
// : db.collection(collectionName).doc();
async function updateRecord(collectionName, docId, data) {
try {
const docRef = db.collection(collectionName).doc(docId);

// await docRef.update(data);
await docRef.update(data);

// return docRef.id;
// } catch (error) {
// console.error('Error updating record:', error);
// throw error;
// }
// }
return docRef.id;
} catch (error) {
console.error('Error updating record:', error);
throw error;
}
}

async function deleteRecord(collectionName, docId) {
try {
Expand Down Expand Up @@ -97,4 +95,10 @@ const createCurriedQuery =
}
};

export { addRecord, readRecord, deleteRecord, createCurriedQuery };
export {
addRecord,
readRecord,
updateRecord,
deleteRecord,
createCurriedQuery,
};

0 comments on commit e0d8a56

Please sign in to comment.