Skip to content

Commit

Permalink
Updated property schema to include discrete propertyUrl attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisischris committed Jul 29, 2024
1 parent 72ff61f commit f4d3c8f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 46 deletions.
8 changes: 2 additions & 6 deletions src/routes/addProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ export const addProperties = async ({ request, reply }) => {

await db.connect();
const id = (await db.query(`
INSERT INTO "properties" ("user_id", "name", "discovery") VALUES ($1, $2, $3) RETURNING "id"
`, [jwtClaims.sub, request.body.propertyName, request.body.propertyDiscovery])).rows?.[0]?.id;
await db.query(
`INSERT INTO "urls" ("user_id", "property_id", "url") VALUES ($1, $2, $3)`,
[jwtClaims.sub, id, `${request.body.propertyUrl}${!request.body.propertyUrl.endsWith('/') ? '/' : ''}`],
);
INSERT INTO "properties" ("user_id", "name", "discovery", "property_url") VALUES ($1, $2, $3, $4) RETURNING "id"
`, [jwtClaims.sub, request.body.propertyName, request.body.propertyDiscovery, `${request.body.propertyUrl}${!request.body.propertyUrl.endsWith('/') ? '/' : ''}`])).rows?.[0]?.id;
await db.clean();

return {
Expand Down
71 changes: 34 additions & 37 deletions src/routes/addScans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,51 @@ export const addScans = async ({ request, reply }) => {

await db.connect();
for (const propertyId of request.body.propertyIds ?? []) {
const propertyDiscovery = (await db.query(`SELECT "discovery" FROM "properties" WHERE "id"=$1`, [propertyId])).rows?.[0]?.discovery;
if (!propertyDiscovery) {
const property = (await db.query(`SELECT "id", "discovery", "property_url" FROM "properties" WHERE "id"=$1`, [propertyId])).rows?.[0];
if (!property.discovery) {
return {
status: 'error',
message: 'One or more of the provided propertyIds is invalid',
}
}
const urls = (await db.query(`SELECT "id", "url" FROM "urls" WHERE "property_id"=$1`, [propertyId])).rows;
for (const { id, url } of urls) {
try {
if (!validateUrl(url)) {
return {
status: 'error',
message: 'One or more of the provided propertyIds has an invalid url',
}
try {
if (!validateUrl(property.property_url)) {
return {
status: 'error',
message: 'One or more of the provided propertyIds has an invalid url',
}
}
else {
const discoveryDict = {
single: 'url',
sitemap: 'sitemapurl',
}
else {
const discoveryDict = {
single: 'url',
sitemap: 'sitemapurl',
}

const scanResponse = await (await fetch(`https://scan.equalify.app/generate/${discoveryDict?.[propertyDiscovery] ?? 'url'}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url: (propertyDiscovery === 'sitemap' && !url.endsWith('.xml')) ? `${url}/sitemap.xml` : url })
})).json();
console.log(JSON.stringify({ scanResponse }));
const scanResponse = await (await fetch(`https://scan.equalify.app/generate/${discoveryDict?.[property.discovery] ?? 'url'}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url: property.property_url })
})).json();
console.log(JSON.stringify({ scanResponse }));

for (const { jobId, url } of scanResponse?.jobs ?? []) {
const urlId = (await db.query({
text: `SELECT "id" FROM "urls" WHERE "user_id"=$1 AND "url"=$2 AND "property_id"=$3`,
values: [jwtClaims.sub, url, propertyId],
})).rows?.[0]?.id ?? (await db.query({
text: `INSERT INTO "urls" ("user_id", "url", "property_id") VALUES ($1, $2, $3) RETURNING "id"`,
values: [jwtClaims.sub, url, propertyId]
})).rows?.[0]?.id;
for (const { jobId, url } of scanResponse?.jobs ?? []) {
const urlId = (await db.query({
text: `SELECT "id" FROM "urls" WHERE "user_id"=$1 AND "url"=$2 AND "property_id"=$3`,
values: [jwtClaims.sub, url, propertyId],
})).rows?.[0]?.id ?? (await db.query({
text: `INSERT INTO "urls" ("user_id", "url", "property_id") VALUES ($1, $2, $3) RETURNING "id"`,
values: [jwtClaims.sub, url, propertyId]
})).rows?.[0]?.id;

await db.query({
text: `INSERT INTO "scans" ("user_id", "property_id", "url_id", "job_id") VALUES ($1, $2, $3, $4) RETURNING "id"`,
values: [jwtClaims.sub, propertyId, urlId, parseInt(jobId)]
});
}
await db.query({
text: `INSERT INTO "scans" ("user_id", "property_id", "url_id", "job_id") VALUES ($1, $2, $3, $4) RETURNING "id"`,
values: [jwtClaims.sub, propertyId, urlId, parseInt(jobId)]
});
}
}
catch (err) {
console.log(err);
}
}
catch (err) {
console.log(err);
}
}
await db.clean();
Expand Down
2 changes: 1 addition & 1 deletion src/routes/getProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const getProperties = async ({ request, reply }) => {
nodes {
id
name
urls: urlsConnection { nodes { url } }
propertyUrl
lastProcessed
archived
discovery
Expand Down
4 changes: 2 additions & 2 deletions src/routes/updateProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export const updateProperties = async ({ request, reply }) => {
SELECT * FROM "properties" WHERE "id"=$1 AND "user_id"=$2
`, [request.body.propertyId, jwtClaims.sub]))?.rows?.[0];
await db.query(`
UPDATE "properties" SET "name"=$1, "propertyUrl"=$2, "discovery"=$3, "archived"=$4, "processed"=$5 WHERE "id"=$6 AND "user_id"=$7
`, [request.body.propertyName ?? original.name, request.body.propertyUrl ?? original.propertyUrl, request.body.propertyDiscovery ?? original.discovery, request.body.propertyArchived ?? original.archived, request.body.propertyProcessed ?? original.processed, request.body.propertyId, jwtClaims.sub]);
UPDATE "properties" SET "name"=$1, "property_url"=$2, "discovery"=$3, "archived"=$4, "processed"=$5 WHERE "id"=$6 AND "user_id"=$7
`, [request.body.propertyName ?? original.name, request.body.propertyUrl ?? original.property_url, request.body.propertyDiscovery ?? original.discovery, request.body.propertyArchived ?? original.archived, request.body.propertyProcessed ?? original.processed, request.body.propertyId, jwtClaims.sub]);
await db.clean();

return {
Expand Down

0 comments on commit f4d3c8f

Please sign in to comment.