diff --git a/src/app.ts b/src/app.ts index fa550e3..5022d6b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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'; @@ -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 })); diff --git a/src/routes/addScansByProperty.ts b/src/routes/addScansByProperty.ts index 32fda9b..d9c0cdb 100644 --- a/src/routes/addScansByProperty.ts +++ b/src/routes/addScansByProperty.ts @@ -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 { diff --git a/src/routes/deletePages.ts b/src/routes/deletePages.ts new file mode 100644 index 0000000..274961f --- /dev/null +++ b/src/routes/deletePages.ts @@ -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, + }; + } +} \ No newline at end of file