-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completed add scans endpoint, decoupled URLs from properties
- Loading branch information
1 parent
b53bbe1
commit 4920e85
Showing
3 changed files
with
66 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters