Skip to content

Commit

Permalink
Completed add scans endpoint, decoupled URLs from properties
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisischris committed Jun 9, 2024
1 parent b53bbe1 commit 4920e85
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/routes/addProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ export const addProperties = async ({ request, reply }) => {

await pgClient.connect();
const id = (await pgClient.query(`
INSERT INTO "properties" ("user_id", "name", "sitemapUrl", "discovery") VALUES ($1, $2, $3, $4) RETURNING "id"
`, [jwtClaims.sub, request.body.propertyName, request.body.sitemapUrl, request.body.discovery])).rows?.[0]?.id;
INSERT INTO "properties" ("user_id", "name", "discovery") VALUES ($1, $2, $3) RETURNING "id"
`, [jwtClaims.sub, request.body.propertyName, request.body.discovery])).rows?.[0]?.id;
await pgClient.query(`INSERT INTO "urls" ("user_id", "property_id", "url") VALUES ($1, $2, $3)`, [jwtClaims.sub, id, request.body.sitemapUrl]);
await pgClient.clean();

return {
Expand Down
61 changes: 58 additions & 3 deletions src/routes/addScans.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,63 @@
import { jwtClaims } from '../app.js';
import { pgClient, validateUuid } from '../utils/index.js';
import { jwtClaims } from 'app.js';
import { pgClient, validateUrl } from '../utils/index.js';

export const addScans = async ({ request, reply }) => {
if (!(request.body.propertyIds || request.body.urlIds)) {
return {
status: 'error',
message: 'PropertyIds and/or urlIds are required.',
}
}

await pgClient.connect();
for (const propertyId of request.body.propertyIds ?? []) {
const propertyExists = (await pgClient.query(`SELECT "id" FROM "properties" WHERE "id"=$1`, [propertyId])).rows?.[0]?.id;
if (!propertyExists) {
return {
status: 'error',
message: 'One or more of the provided propertyIds is invalid',
}
}

const urls = (await pgClient.query(`SELECT "id", "url" FROM "urls" WHERE "property_id"=$1`, [propertyId])).rows;
for (const { id, url } of urls) {
if (!validateUrl(url)) {
return {
status: 'error',
message: 'One or more of the provided propertyIds has an invalid url',
}
}
else {
await pgClient.query(`
INSERT INTO "scans" ("user_id", "property_id", "url_id") VALUES ($1, $2, $3) RETURNING "id"
`, [jwtClaims.sub, propertyId, id]);
}
}
}
for (const urlId of request.body.urlIds ?? []) {
const url = (await pgClient.query(`SELECT "url" FROM "urls" WHERE "id"=$1`, [urlId])).rows?.[0]?.url;
if (!url) {
return {
status: 'error',
message: 'One or more of the provided urlIds is invalid',
}
}
else if (!validateUrl(url)) {
return {
status: 'error',
message: 'One or more of the provided urlIds is invalid',
}
}
else {
await pgClient.query(`
INSERT INTO "scans" ("user_id", "url_id") VALUES ($1, $2) RETURNING "id"
`, [jwtClaims.sub, urlId]);
}
}
await pgClient.clean();
return;

return {
status: 'success',
message: 'Scans successfully queued',
};
}
6 changes: 5 additions & 1 deletion src/routes/getProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export const getProperties = async ({ request, reply }) => {
nodes {
id
name
sitemapUrl
urls {
nodes {
url
}
}
lastProcessed
archived
discovery
Expand Down

0 comments on commit 4920e85

Please sign in to comment.