Skip to content

Commit

Permalink
Log Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtyson123 committed Feb 22, 2024
1 parent 4102c8c commit 9ced66c
Show file tree
Hide file tree
Showing 23 changed files with 40 additions and 119 deletions.
1 change: 0 additions & 1 deletion website/src/components/infinteLoading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export function InfiniteLoading({searchQuery} : InfiniteLoadingProps) {
useEffect(() => {
if(entry?.isIntersecting){
fetchNextPage()
console.log("Fetching next page")
}
}, [entry])

Expand Down
1 change: 0 additions & 1 deletion website/src/components/input_sections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,6 @@ export function FileInput({placeHolder, defaultValue, required, state, errorTex

// If there is no file selected then return
if (!selectedFile) {
console.log('Please select a file.');
return;
}

Expand Down
2 changes: 1 addition & 1 deletion website/src/lib/databse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export async function makeQuery(query: string, client: any, rawData : boolean =
return data

} catch (e) {
console.log("ERROR")
console.log("DATABASE ERROR: ")
console.log(e)
return null
}
Expand Down
1 change: 0 additions & 1 deletion website/src/lib/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ export function getUserPermissions(user: RongoaUser | null) {
// If there is no user logged in it must be a guest
if(user == null)
{
console.log("User is guest");
return permissions;
}

Expand Down
2 changes: 1 addition & 1 deletion website/src/pages/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default function Admin(){
setLoading(false)

// Log that the user has logged in
log.info("User access admin database:", {user: session?.user?.name, email: session?.user?.email})
log.info("User access admin database: " + session?.user?.email)
}

const adminPage = () => {
Expand Down
3 changes: 3 additions & 0 deletions website/src/pages/admin/plants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ export default function Admin(){
// Send the remove request
const response = await makeRequestWithToken("get", "/api/plants/remove?id=" + id)

// Log that the admin has deleted the plant
log.info(`Admin ${session?.user?.email} has deleted the plant with id ${id}`)

// Clear the cache
localStorage.removeItem("plant_admin_data")
localStorage.removeItem("plant_stats")
Expand Down
7 changes: 5 additions & 2 deletions website/src/pages/admin/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,14 @@ export default function Admin(){
setLoading(false)
}

// Log that the admin imported a back up
log.info(`Admin ${session?.user?.email} imported back up`)

// Clear the cache
clearCache()

// Go back to the admin page
router.push("/admin")
await router.push("/admin")

} catch (error) {
setLoading(false)
Expand All @@ -156,7 +159,7 @@ export default function Admin(){
}


const clearCache = async () => {
const clearCache = () => {
localStorage.clear()
window.location.reload()
}
Expand Down
4 changes: 4 additions & 0 deletions website/src/pages/admin/users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,16 @@ export default function Admin(){

const response = await makeRequestWithToken("get", "/api/user/update?adminData=" + JSON.stringify(adminData))

// Log that the admin has updated the user
log.info(`Admin ${session?.user?.email} has updated the user with id ${id}`)

// Remove the item in the local storage
localStorage.removeItem("user_admin_data")

setLoading(false)
}

// TODO: Delete user . ban

const reload = () => {
window.location.reload()
Expand Down
8 changes: 0 additions & 8 deletions website/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ export const authOptions: NextAuthOptions = {
// If there isnt the user data in the token the fetch it
if(!token.user) {

console.log("Refresh");
// Get the tables and client
const tables = getTables();
const client = await getClient()

// Make the query
let query = `SELECT * FROM users WHERE ${tables.user_email} = '${token.email}'`;
console.log("DATABASE: "+ query);
const user = await makeQuery(query, client)

if(user.length != 0) {
Expand All @@ -47,11 +45,9 @@ export const authOptions: NextAuthOptions = {

if (trigger === "update" && session?.database) {

console.log("Update");
token.user = session.database;
// @ts-ignore
token.user.user_last_login = new Date().toISOString();
console.log(token.user);
}

return token
Expand All @@ -76,15 +72,13 @@ export const authOptions: NextAuthOptions = {

// Check if there already is a user
let query = `SELECT * FROM users WHERE ${tables.user_email} = '${user_email}'`;
console.log("DATABASE: "+ query);
const existing_user = await makeQuery(query, client)
if(existing_user.length > 0) {

logger.info(`User logged in with email ${user_email}`);

// Update the users login time and image
query = `UPDATE users SET ${tables.user_last_login} = NOW() WHERE ${tables.user_email} = '${user_email}'`;
console.log("DATABASE: "+ query);
await makeQuery(query, client)

return true
Expand All @@ -99,8 +93,6 @@ export const authOptions: NextAuthOptions = {
return true

} catch (error) {
console.log("Error");
console.log(error);

// If there is an error, return the error
return false
Expand Down
11 changes: 6 additions & 5 deletions website/src/pages/api/files/backup_database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ export default async function handler(
// Get the information for the source data
query += `SELECT * FROM source;`;

// Get the information for the user data
query += `SELECT * FROM users;`;

// Get the information for the api key data
query += `SELECT * FROM api_key;`;

// Finally Get the information for the plant data
query += `SELECT * FROM plants;`;

// Log the query
console.log("DATABASE: "+ query);

// Log the backup request
logger.info(`Backup request by ${session?.user?.email}`);
Expand All @@ -72,9 +76,6 @@ export default async function handler(


} catch (error) {
console.log("Error");
console.log(error);

// If there is an error, return the error
return response.status(500).json({ error: error });
}
Expand Down
1 change: 0 additions & 1 deletion website/src/pages/api/files/backup_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ export default async function handler(
// Get the client
const client = await getClient()


// Check if the user has the correct permissions
const session = await getServerSession(request, response, authOptions)
const permission = await checkApiPermissions(request, response, session, client, makeQuery, "api:files:backup_files:access")
Expand Down
10 changes: 2 additions & 8 deletions website/src/pages/api/files/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,10 @@ export default async function handler(
// Get the path to upload to
const folder = `/${path}/${id}`
const remotePath = `${folder}/${file[0].originalFilename}`
console.log(remotePath)


const directories = folder.split('/').filter(Boolean);
let currentDir = '';

console.log("Creating directories")
directories.forEach((directory) => {
currentDir += `/${directory}`;
ftp.mkdir(currentDir, (ftpErr) => {
Expand All @@ -320,15 +317,15 @@ export default async function handler(

// If there is an error return it
if (ftpErr) {
console.log(ftpErr)
return response.status(500).json({ error: 'Error uploading file.', data: ftpErr });
}

// Delete the file locally
fs.unlinkSync(file[0].path);

// Log the upload
console.log(`File uploaded to ${remotePath} by ${session?.user?.email}`);
const logger = new Logger()
logger.info(`File uploaded to ${remotePath} by ${session?.user?.email}`);

// Return that the file was uploaded
return response.status(200).json({ message: 'File uploaded successfully.', path: remotePath });
Expand All @@ -345,13 +342,10 @@ export default async function handler(
});
}
catch(err) {
console.log(err)
return response.status(500).json({message: "ERROR IN SERVER", error: err });
}
} catch (error) {
// If there is an error, return the error
console.log("ERROR")
console.log(error)
return response.status(500).json({message: "ERROR IN SERVER", error: error });
}
}
Expand Down
12 changes: 0 additions & 12 deletions website/src/pages/api/plants/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,19 +258,9 @@ export async function downloadPlantData(table: any, id: any, client: any, restri
${joiner};
`;

// Log the query

console.log("DATABASE: "+ query);


// Make the query
const data = await makeQuery(query, client)

// Debug
console.log("DATA")
console.log(data)
console.log("DONE")

// If the data is empty, return an error
if(!data)
return ["error", "No data found"];
Expand All @@ -279,8 +269,6 @@ export async function downloadPlantData(table: any, id: any, client: any, restri
return ["success", data];

} catch (error) {
console.log(error)

// If there is an error, return the error
return ["error", error];
}
Expand Down
5 changes: 0 additions & 5 deletions website/src/pages/api/plants/months.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,8 @@ export default async function handler(
JOIN plants ON plants.id = months_ready_for_use.plant_id;
`;


console.log("DATABASE: "+ query);


// Get the data from the database
const data = await makeQuery(query, client);
console.log(data);

// If the data is empty, return an error
if(!data) {
Expand Down
41 changes: 12 additions & 29 deletions website/src/pages/api/plants/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {getClient, makeQuery, PostgresSQL, SQLDatabase} from "@/lib/databse";
import {getServerSession} from "next-auth";
import {authOptions} from "@/pages/api/auth/[...nextauth]";
import {checkApiPermissions} from "@/lib/api_tools";
import {Logger} from "next-axiom";


export default async function handler(
Expand Down Expand Up @@ -49,45 +50,27 @@ export default async function handler(
// Create the query
let query = ``;

// Remove the information for attachment data
query += `DELETE FROM attachments WHERE plant_id = ${id};`;
// Define the tables to delete from
const tablesToDeleteFrom = [ "attachments", "craft", "custom", "edible", "medical", "months_ready_for_use", "source" ];

// Remove the information for the craft data
query += `DELETE FROM craft WHERE plant_id = ${id};`;

// Remove the information for the custom data
query += `DELETE FROM custom WHERE plant_id = ${id};`;

// Remove the information for the edible data
query += `DELETE FROM edible WHERE plant_id = ${id};`;

// Remove the information for the medical data
query += `DELETE FROM medical WHERE plant_id = ${id};`;

// Remove the information for the months ready for use data
query += `DELETE FROM months_ready_for_use WHERE plant_id = ${id};`;

// Remove the information for the source data
query += `DELETE FROM source WHERE plant_id = ${id};`;
// Remove the information
for (let table of tablesToDeleteFrom) {
query += `DELETE FROM ${table} WHERE plant_id = ${id};`;
}

// Finally Remove the information for the plant data
query += `DELETE FROM plants WHERE id = ${id};`;

// Log the query

console.log("DATABASE: "+ query);


// Log the deletion
console.log(`Plant ${id} removed by ${session?.user?.email}`);

// Remove the plant
const data = await makeQuery(query, client)

// Get the logger
const logger = new Logger()
logger.warn(`Plant removed by ${session?.user?.email} with id ${id}`)

return response.status(200).json({ message: "Remove sent", id: id });

} catch (error) {
console.log("Error");
console.log(error);

// If there is an error, return the error
return response.status(500).json({ error: error });
Expand Down
4 changes: 0 additions & 4 deletions website/src/pages/api/plants/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ export default async function handler(
query += ` LIMIT ${amountPerPage} OFFSET ${(currentPage - 1) * amountPerPage}`
}

console.log("=================================")
console.log(query)
console.log("=================================")

// Return the plants that match the query
const plantIds = await makeQuery(query, client)

Expand Down
17 changes: 1 addition & 16 deletions website/src/pages/api/plants/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ export default async function handler(

} = request.body;

console.log("API/Upload")
console.log(request.body);

// Check if the data is being downloaded from the Postgres database
const tables = getTables()

Expand Down Expand Up @@ -250,18 +247,9 @@ export default async function handler(

}

// Log the query

console.log("DATABASE: "+ query);


// Run the query
const data = await makeQuery(query, client, true)

// Log the data
console.log("DATA")
console.log(data)

if(!data)
return response.status(500).json({ error: "No data returned" });

Expand Down Expand Up @@ -297,13 +285,10 @@ export default async function handler(
}

// Log the upload
logger.info(`Plant ${id} uploaded by ${session?.user?.email}`);
logger.info(`Plant ${id} created by ${session?.user?.email}`);

return response.status(200).json({ message: "Upload Successful", id: id });
} catch (error) {
// If there is an error, return the error
console.log("ERROR")
console.log(error)
return response.status(500).json({message: "ERROR IN SERVER", error: error });
} finally {

Expand Down
Loading

0 comments on commit 9ced66c

Please sign in to comment.