Skip to content

Commit

Permalink
required grant in api response
Browse files Browse the repository at this point in the history
  • Loading branch information
lengyel-arpad85 committed Dec 16, 2024
1 parent 07183bc commit eb14c77
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
17 changes: 16 additions & 1 deletion backend/src/controllers/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
getS3AndParams,
streamToString
} from '../services/utils.js'
import { S3FileNotFoundError } from '../services/errors.js'
import { MissingGrantError, S3FileNotFoundError } from '../services/errors.js'

export const getDefault = async (_: Request, res: Response) => {
try {
Expand Down Expand Up @@ -36,6 +36,12 @@ export const saveUserConfig = async (req: Request, res: Response) => {
try {
// existing config
const s3data = await s3.send(new GetObjectCommand(params))

// if file exists, we need to verify ownership
if (!data.variableHoldingSomeConfirmation) {
throw new MissingGrantError('Grant confirmation is required')
}

// Convert the file stream to a string
fileContentString = await streamToString(
s3data.Body as NodeJS.ReadableStream
Expand All @@ -44,9 +50,18 @@ export const saveUserConfig = async (req: Request, res: Response) => {
const err = error as Error
if (err.name === 'NoSuchKey') {
// file / config not found, continue with defaults
} else if (err.name === 'MissingGrant') {
res
.status(200)
.send({
grantRequired: true,
message: 'Grant confirmation is required'
})
return
} else {
console.log(error)
res.status(500).send('An error occurred while fetching data')
return
}
}

Expand Down
7 changes: 7 additions & 0 deletions backend/src/services/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ export class S3FileNotFoundError extends Error {
this.name = 'NoSuchKey' // Set the error name
}
}

export class MissingGrantError extends Error {
constructor(message: string) {
super(message)
this.name = 'MissingGrant'
}
}
9 changes: 8 additions & 1 deletion frontend/app/routes/create.$type.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ export default function Create() {
useEffect(() => {
const errors = Object.keys(response?.errors?.fieldErrors || {})

if (response && !errors.length && response.displayScript) {
if (
response &&
response.apiResponse &&
response.apiResponse.payload &&
response.apiResponse.payload.grantRequired
) {
/// code comes here
} else if (response && !errors.length && response.displayScript) {
setModalOpen(true)
} else if (
response &&
Expand Down

0 comments on commit eb14c77

Please sign in to comment.