Skip to content

Commit

Permalink
Added URL & Discovery validation, hooked up add/update/delete reports
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisischris committed May 2, 2024
1 parent 33c3a09 commit 338512f
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 13 deletions.
16 changes: 14 additions & 2 deletions src/routes/addProperties.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { jwtClaims } from '../app.js';
import { pgClient } from '../utils/index.js';
import { formatId, pgClient, validateDiscovery, validateDiscoveryOptions, validateUrl } from '../utils/index.js';

export const addProperties = async ({ request, reply }) => {
if (!request.body.propertyName) {
Expand All @@ -20,6 +20,18 @@ export const addProperties = async ({ request, reply }) => {
message: 'Property Discovery settings are required.',
}
}
else if (!validateUrl(request.body.propertyUrl)) {
return {
status: 'error',
message: 'Property URL is not valid.',
}
}
else if (!validateDiscovery(request.body.propertyDiscovery)) {
return {
status: 'error',
message: `Property Discovery is not valid- must be one of: ${validateDiscoveryOptions.join(', ')}`,
}
}

await pgClient.connect();
const id = (await pgClient.query(`
Expand All @@ -30,6 +42,6 @@ export const addProperties = async ({ request, reply }) => {
return {
status: 'success',
message: 'Property added successfully',
result: id,
result: formatId(id),
};
}
25 changes: 23 additions & 2 deletions src/routes/addReports.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
import { jwtClaims } from '../app.js';
import { pgClient, validateUuid } from '../utils/index.js';
import { formatId, pgClient } from '../utils/index.js';

export const addReports = async ({ request, reply }) => {
if (!request.body.reportName) {
return {
status: 'error',
message: 'Report Name is required.',
}
}
else if (!request.body.reportFilters) {
return {
status: 'error',
message: 'Report filters are required.',
}
}

await pgClient.connect();
const id = (await pgClient.query(`
INSERT INTO "reports" ("user_id", "name", "filters") VALUES ($1, $2, $3) RETURNING "id"
`, [jwtClaims.sub, request.body.reportName, request.body.reportFilters])).rows?.[0]?.id;
await pgClient.clean();
return;

return {
status: 'success',
message: 'Report added successfully',
result: formatId(id),
};
}
6 changes: 3 additions & 3 deletions src/routes/deleteReports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ export const deleteReports = async ({ request, reply }) => {
if (!request.query.reportId) {
return {
status: 'error',
message: 'Property ID is required.',
message: 'Report ID is required.',
}
}
else if (!validateUuid(request.query.reportId)) {
return {
status: 'error',
message: 'Property ID is not a valid UUID.',
message: 'Report ID is not a valid UUID.',
}
}

Expand All @@ -30,7 +30,7 @@ export const deleteReports = async ({ request, reply }) => {
else {
return {
status: 'success',
message: 'Property deletion successful',
message: 'Report deletion successful',
result: deletedIds,
};
}
Expand Down
7 changes: 5 additions & 2 deletions src/routes/getReports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { graphqlQuery } from '../utils/index.js';
export const getReports = async ({ request, reply }) => {
const response = (await graphqlQuery({
query: `query($first: Int, $offset: Int){
reports(first: $first, offset: $offset) {
reports(first: $first, offset: $offset, ${(request.query.reportIds) ? `filter: { id: {in: [
${request.query.reportIds.split(',').map(obj => `"${obj}"`).join()}
]}}` : ''}
) {
nodes {
id
title
name
filters
}
totalCount
Expand Down
15 changes: 13 additions & 2 deletions src/routes/updateProperties.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { jwtClaims } from '../app.js';
import { pgClient, validateUuid } from '../utils/index.js';
import { pgClient, validateDiscovery, validateDiscoveryOptions, validateUrl, validateUuid } from '../utils/index.js';

export const updateProperties = async ({ request, reply }) => {
if (!validateUuid(request.body.propertyId)) {
Expand All @@ -14,12 +14,23 @@ export const updateProperties = async ({ request, reply }) => {
message: 'At least propertyName, propertyUrl, and/or propertyDiscovery are required.',
}
}
else if (request.body.propertyUrl && !validateUrl(request.body.propertyUrl)) {
return {
status: 'error',
message: 'PropertyUrl is not valid.',
}
}
else if (request.body.propertyDiscovery && !validateDiscovery(request.body.propertyDiscovery)) {
return {
status: 'error',
message: `propertyDiscovery is not valid- must be one of these values: ${validateDiscoveryOptions.join(', ')}`,
}
}

await pgClient.connect();
const original = (await pgClient.query(`
SELECT * FROM "properties" WHERE "id"=$1 AND "user_id"=$2
`, [request.body.propertyId, jwtClaims.sub]))?.rows?.[0];
console.log(original);
await pgClient.query(`
UPDATE "properties" SET "name"=$1, "url"=$2, "discovery"=$3, "archived"=$4, "processed"=$5 WHERE "id"=$6 AND "user_id"=$7
`, [request.body.propertyName ?? original.name, request.body.propertyUrl ?? original.url, request.body.propertyDiscovery ?? original.discovery, request.body.propertyArchived ?? original.archived, request.body.propertyProcessed ?? original.processed, request.body.propertyId, jwtClaims.sub]);
Expand Down
25 changes: 24 additions & 1 deletion src/routes/updateReports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,30 @@ import { jwtClaims } from '../app.js';
import { pgClient, validateUuid } from '../utils/index.js';

export const updateReports = async ({ request, reply }) => {
if (!validateUuid(request.body.reportId)) {
return {
status: 'error',
message: 'Report ID is not a valid UUID.',
}
}
else if (!(request.body.reportName || request.body.reportFilters)) {
return {
status: 'error',
message: 'At least reportName and/or reportFilters are required.',
}
}

await pgClient.connect();
const original = (await pgClient.query(`
SELECT * FROM "reports" WHERE "id"=$1 AND "user_id"=$2
`, [request.body.reportId, jwtClaims.sub]))?.rows?.[0];
await pgClient.query(`
UPDATE "reports" SET "name"=$1, "filters"=$2 WHERE "id"=$3 AND "user_id"=$4
`, [request.body.reportName ?? original.name, request.body.reportFilters ?? original.url, request.body.reportId, jwtClaims.sub]);
await pgClient.clean();
return;

return {
status: 'success',
message: 'Report updated successfully',
};
}
1 change: 1 addition & 0 deletions src/utils/formatId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const formatId = (id) => id.replaceAll('-', '')
5 changes: 4 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export * from './graphqlQuery.js'
export * from './pgClient.js'
export * from './openai.js'
export * from './validateUuid.js'
export * from './validateUuid.js'
export * from './validateUrl.js'
export * from './validateDiscovery.js'
export * from './formatId.js'
2 changes: 2 additions & 0 deletions src/utils/validateDiscovery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const validateDiscoveryOptions = ['single_page_import', 'sitemap_import', 'manually_added']
export const validateDiscovery = (value) => validateDiscoveryOptions.includes(value)
10 changes: 10 additions & 0 deletions src/utils/validateUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { URL } from 'url'

export const validateUrl = (s) => {
try {
new URL(s);
return true;
} catch (err) {
return false;
}
};

0 comments on commit 338512f

Please sign in to comment.