-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/webforms' into silky
- Loading branch information
Showing
52 changed files
with
1,374 additions
and
186 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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum Action { | ||
ENABLE_RAI_WITHDRAW = "enable-rai-withdraw", | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { APIGatewayEvent } from "aws-lambda"; | ||
import { Action, CognitoUserAttributes, ItemResult } from "shared-types"; | ||
import { isCmsUser } from "shared-utils"; | ||
import { getPackage } from "../libs/package/getPackage"; | ||
import { | ||
getAuthDetails, | ||
isAuthorized, | ||
lookupUserAttributes, | ||
} from "../libs/auth/user"; | ||
import { response } from "../libs/handler"; | ||
|
||
type GetPackageActionsBody = { | ||
id: string; | ||
}; | ||
|
||
/** Generates an array of allowed actions from a combination of user attributes | ||
* and OS result data */ | ||
const packageActionsForResult = ( | ||
user: CognitoUserAttributes, | ||
result: ItemResult | ||
): Action[] => { | ||
const actions = []; | ||
if (isCmsUser(user)) { | ||
actions.push(Action.ENABLE_RAI_WITHDRAW); | ||
} | ||
return actions; | ||
}; | ||
export const getPackageActions = async (event: APIGatewayEvent) => { | ||
const body = JSON.parse(event.body) as GetPackageActionsBody; | ||
try { | ||
console.log(body); | ||
const result = await getPackage(body.id); | ||
const passedStateAuth = await isAuthorized(event, result._source.state); | ||
if (!passedStateAuth) | ||
return response({ | ||
statusCode: 401, | ||
body: { message: "Not authorized to view resources from this state" }, | ||
}); | ||
if (!result.found) | ||
return response({ | ||
statusCode: 404, | ||
body: { message: "No record found for the given id" }, | ||
}); | ||
|
||
const authDetails = getAuthDetails(event); | ||
const userAttr = await lookupUserAttributes( | ||
authDetails.userId, | ||
authDetails.poolId | ||
); | ||
|
||
return response({ | ||
statusCode: 200, | ||
body: { | ||
actions: packageActionsForResult(userAttr, result), | ||
}, | ||
}); | ||
} catch (err) { | ||
console.error({ err }); | ||
return response({ | ||
statusCode: 500, | ||
body: { message: "Internal server error" }, | ||
}); | ||
} | ||
}; | ||
|
||
export const handler = getPackageActions; |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { response } from "../libs/handler"; | ||
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3"; | ||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
checkEnvVariables(["attachmentsBucketName", "attachmentsBucketRegion"]); | ||
|
||
const s3 = new S3Client({ | ||
region: process.env.attachmentsBucketRegion, | ||
}); | ||
|
||
export const handler = async () => { | ||
try { | ||
const bucket = process.env.attachmentsBucketName; | ||
const key = uuidv4(); | ||
const url = await getSignedUrl( | ||
s3, | ||
new PutObjectCommand({ | ||
Bucket: bucket, | ||
Key: key, | ||
}), | ||
{ | ||
expiresIn: 60, | ||
} | ||
); | ||
|
||
return response<unknown>({ | ||
statusCode: 200, | ||
body: { url, bucket, key }, | ||
}); | ||
} catch (error) { | ||
console.error({ error }); | ||
return response({ | ||
statusCode: 500, | ||
body: { message: "Internal server error" }, | ||
}); | ||
} | ||
}; | ||
|
||
function checkEnvVariables(requiredEnvVariables) { | ||
const missingVariables = requiredEnvVariables.filter( | ||
(envVar) => !process.env[envVar] | ||
); | ||
|
||
if (missingVariables.length > 0) { | ||
throw new Error( | ||
`Missing required environment variables: ${missingVariables.join(", ")}` | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import * as os from "../../../../libs/opensearch-lib"; | ||
import { ItemResult } from "shared-types"; | ||
|
||
export const getPackage = async (id: string) => | ||
(await os.getItem(process.env.osDomain, "main", id)) as ItemResult; |
Oops, something went wrong.