Skip to content

Commit

Permalink
add clear user db
Browse files Browse the repository at this point in the history
  • Loading branch information
birdpump committed May 23, 2024
1 parent d681e52 commit 355ca81
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
27 changes: 17 additions & 10 deletions controllers/admin/adminData.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {readConfig} from "../../utils/admin/configManager.js";
import {throwApplicationError} from "../../middleware/errorHandler.js";



//todo fix try catch here
export async function queryGradeRestriction() {
try {
Expand Down Expand Up @@ -121,18 +120,18 @@ export async function getLockersDB() {
]);
}

export async function getUserEditData(userID){
export async function getUserEditData(userID) {
return await User.findByPk(userID);
}

export async function getLockerEditData(lockerNum){
export async function getLockerEditData(lockerNum) {
return await Locker.findByPk(lockerNum);
}

export async function updateUserEditData(userID, data){
export async function updateUserEditData(userID, data) {
const user = await User.findByPk(userID);

await user.update({
await user.update({
studentId: data.studentId,
grade: data.grade,
permissions: data.permissions,
Expand All @@ -141,15 +140,15 @@ export async function updateUserEditData(userID, data){
});
}

export async function updateLockerEditData(lockerNum, data){
export async function updateLockerEditData(lockerNum, data) {
const locker = await Locker.findByPk(lockerNum);

await locker.update({
await locker.update({
status: data.status,
});
}

export async function deleteUser(studentId){
export async function deleteUser(studentId) {
let user = await User.findByPk(studentId);
if (user) {
await user.destroy();
Expand All @@ -158,7 +157,7 @@ export async function deleteUser(studentId){
}
}

export async function removeLockerFromUser(studentId){
export async function removeLockerFromUser(studentId) {
let user = await User.findByPk(studentId);
if (user) {
await user.setLocker(null);
Expand All @@ -167,8 +166,16 @@ export async function removeLockerFromUser(studentId){
}
}

export async function manualCreateUser(data){
export async function manualCreateUser(data) {
await createUser(data.studentId, data.name, data.grade, data.permissions, data.email);

//todo user should also be added to userData
}

export async function clearUserDB() {
await User.destroy({
truncate: true,
cascade: true,
restartIdentity: true
});
}
10 changes: 5 additions & 5 deletions controllers/admin/adminImportData.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const formatLockerData = (obj) => {

async function createLockerBatch(data) {
try {
// await Locker.destroy({
// truncate: true,
// cascade: false,
// restartIdentity: true
// });
await Locker.destroy({
truncate: true,
cascade: true,
restartIdentity: true
});
return await Locker.bulkCreate(data);
} catch (err) {
console.error(err);
Expand Down
14 changes: 13 additions & 1 deletion routes/admin/adminData.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
updateLockerEditData,
deleteUser,
removeLockerFromUser,
manualCreateUser
manualCreateUser,
clearUserDB
} from "../../controllers/admin/adminData.js";

import {ensureAuthenticated} from "./adminAuth.js";
Expand Down Expand Up @@ -181,6 +182,17 @@ adminRouter.post('/manual/create-user', async (req, res, next) => {
});


adminRouter.post('/db-action/clear-users', async (req, res, next) => {
try {
await clearUserDB();
res.sendStatus(200);
} catch (error) {
next(error);
}
});



//todo maybe move file upload and handling to adminData.js
const lockerStorage = multer.memoryStorage(); // Store the file in memory
const lockerUpload = multer({storage: lockerStorage});
Expand Down

0 comments on commit 355ca81

Please sign in to comment.