Skip to content

Commit

Permalink
Added delete/pages endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
azdak committed Dec 20, 2024
1 parent 759e06a commit ad6d2c6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Fastify from 'fastify';
import { updatePropertyOnPages, addPages, getScan, addProperties, addReports, addResults, addScans, deleteProperties, deleteReports, deleteUser, getApikey, getCharts, getFilters, getPage, getProperties, getReports, getResultsAll, getResultsMessages, getResultsSchema, getResultsTags, getResultsUrls, getScans, getUpdates, help, trackUser, updateProperties, updateReports, getPages, addScansByPage, getPagesByProperty, addScansByProperty } from '#src/routes';
import { updatePropertyOnPages, addPages, getScan, addProperties, addReports, addResults, addScans, deleteProperties, deleteReports, deleteUser, getApikey, getCharts, getFilters, getPage, getProperties, getReports, getResultsAll, getResultsMessages, getResultsSchema, getResultsTags, getResultsUrls, getScans, getUpdates, help, trackUser, updateProperties, updateReports, getPages, addScansByPage, getPagesByProperty, addScansByProperty, deletePages } from '#src/routes';
import { CognitoJwtVerifier } from 'aws-jwt-verify';
import { db } from './utils';
import { suggestIssue } from './routes/suggestIssue';
Expand Down Expand Up @@ -70,6 +70,7 @@ fastify.put('/update/pages/property', {}, async(request, reply) => updatePropert
fastify.delete('/delete/properties', {}, async (request, reply) => deleteProperties({ request, reply }));
fastify.delete('/delete/reports', {}, async (request, reply) => deleteReports({ request, reply }));
fastify.delete('/delete/user', {}, async (request, reply) => deleteUser({ request, reply }));
fastify.delete('/delete/pages', {}, async (request, reply) => deletePages({ request, reply }));

// MISC requests
fastify.post('/help', {}, async (request, reply) => help({ request, reply }));
Expand Down
7 changes: 7 additions & 0 deletions src/routes/addScansByProperty.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { jwtClaims } from "#src/app";
import { db, graphql, isStaging, validateUrl, validateUuid } from "#src/utils";

/*
Send property to scan
input:
propertyId: UUID
*/

export const addScansByProperty = async ({ request, reply }) => {
if (!request.body.propertyId) {
return {
Expand Down
48 changes: 48 additions & 0 deletions src/routes/deletePages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { jwtClaims } from '#src/app';
import { db, validateUrl, validateUuid } from '#src/utils';

export const deletePages = async ({ request, reply }) => {
const req = request.body;

if (!Array.isArray(req.pageIds)) {
return {
status: "error",
message: `${req.urls.stringify()} is not an array of URLs.`
};
} else {
for (const url of req.pageIds) {
if (!validateUrl(url)) {
return {
status: "error",
message: `${url} is not a valid url.`,
};
}
}
}

await db.connect();

let deletedIds = [];
for (const url of req.pageIds) {
const deletedId = (await db.query(`
DELETE FROM "urls" WHERE "id"=$1 AND "user_id"=$2 RETURNING "id"
`, [request.query.ageId, jwtClaims.sub])).rows.map(obj => obj.id);
deletedIds.push(deletedId);
};

await db.clean();

if (deletedIds.length === 0) {
return {
status: 'error',
message: 'There was an error. No pages deleted.',
}
}
else {
return {
status: 'success',
message: `${deletedIds.length} pages deleted successfully.`,
result: deletedIds,
};
}
}

0 comments on commit ad6d2c6

Please sign in to comment.