-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dynamo db): integrate with dynamo db (#770)
* feat(step functions service): add step functions service * feat(dynamo cb client): add get all items function * feat(config): add env var for step functions * feat(dynamodb): integration with infra service * test(dynamodb service): add test cases * fix(infra service): fix wrong feature flag * style(infra service): use env var rather than inline const * feat(dynamodb service): add type guard * fix(typeguard): fix type guard * style(env var): change naming * fix(typeguard): check for undefined values * style(env var): better naming * fix(config): give default value for step functions arn * style(dynamoDb service): add args to constructor for ease of testing * fix(infra service): add awaits * fix(step functions): region should come from env vars * fix(infra service): handle error appropriately
- Loading branch information
1 parent
5b0a50f
commit b768fea
Showing
8 changed files
with
292 additions
and
134 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
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,82 +1,56 @@ | ||
import { | ||
DeleteCommandOutput, | ||
GetCommandOutput, | ||
UpdateCommandOutput, | ||
} from "@aws-sdk/lib-dynamodb" | ||
import { AttributeValue } from "@aws-sdk/client-dynamodb" | ||
import { DeleteCommandOutput } from "@aws-sdk/lib-dynamodb" | ||
import autoBind from "auto-bind" | ||
|
||
import { config } from "@config/config" | ||
|
||
import { SiteLaunchMessage } from "@root/../microservices/site-launch/shared/types" | ||
import { | ||
SiteLaunchMessage, | ||
isSiteLaunchMessage, | ||
} from "@root/../microservices/site-launch/shared/types" | ||
|
||
import DynamoDBClient, { UpdateParams } from "./DynamoDBClient" | ||
import DynamoDBClient from "./DynamoDBClient" | ||
|
||
const MOCK_LAUNCH: SiteLaunchMessage = { | ||
repoName: "my-repo", | ||
appId: "my-app", | ||
primaryDomainSource: "example.com", | ||
primaryDomainTarget: "myapp.example.com", | ||
domainValidationSource: "example.com", | ||
domainValidationTarget: "myapp.example.com", | ||
requestorEmail: "[email protected]", | ||
agencyEmail: "[email protected]", | ||
githubRedirectionUrl: "https://github.com/my-repo", | ||
redirectionDomain: [ | ||
{ | ||
source: "example.com", | ||
target: "myapp.example.com", | ||
type: "A", | ||
}, | ||
], | ||
status: { state: "pending", message: "PENDING_DURING_SITE_LAUNCH" }, | ||
} | ||
export default class DynamoDBService { | ||
private readonly dynamoDBClient: DynamoDBClient | ||
|
||
private readonly TABLE_NAME: string | ||
|
||
constructor() { | ||
this.dynamoDBClient = new DynamoDBClient() | ||
this.TABLE_NAME = config.get("aws.dynamodb.siteLaunchTableName") | ||
constructor({ | ||
dynamoDBClient, | ||
dynamoDbTableName = config.get("aws.dynamodb.siteLaunchTableName"), | ||
}: { | ||
dynamoDBClient: DynamoDBClient | ||
dynamoDbTableName?: string | ||
}) { | ||
this.dynamoDBClient = dynamoDBClient | ||
this.TABLE_NAME = dynamoDbTableName | ||
autoBind(this) | ||
} | ||
|
||
async createItem(message: SiteLaunchMessage): Promise<void> { | ||
await this.dynamoDBClient.createItem(this.TABLE_NAME, MOCK_LAUNCH) | ||
await this.dynamoDBClient.createItem(this.TABLE_NAME, message) | ||
} | ||
|
||
async getItem(message: SiteLaunchMessage): Promise<GetCommandOutput> { | ||
return this.dynamoDBClient.getItem(this.TABLE_NAME, MOCK_LAUNCH.appId) | ||
} | ||
async getAllCompletedLaunches(): Promise<SiteLaunchMessage[]> { | ||
const entries = (( | ||
await this.dynamoDBClient.getAllItems(this.TABLE_NAME) | ||
).Items?.filter(isSiteLaunchMessage) as unknown) as SiteLaunchMessage[] | ||
|
||
const completedEntries = | ||
entries?.filter( | ||
(entry) => | ||
entry.status?.state === "success" || entry.status?.state === "failure" | ||
) || [] | ||
|
||
async updateItem(message: SiteLaunchMessage): Promise<UpdateCommandOutput> { | ||
// TODO: delete mocking after integration in IS-186 | ||
MOCK_LAUNCH.status = { state: "success", message: "SUCCESS_SITE_LIVE" } | ||
const updateParams: UpdateParams = { | ||
TableName: this.TABLE_NAME, | ||
Key: { appId: MOCK_LAUNCH.appId }, | ||
// The update expression to apply to the item, | ||
// in this case setting the "status" attribute to a value | ||
UpdateExpression: | ||
"set #status.#state = :state, #status.#message = :message", | ||
ExpressionAttributeNames: { | ||
"#status": "status", | ||
"#state": "state", | ||
"#message": "message", | ||
}, | ||
// A map of expression attribute values used in the update expression, | ||
// in this case mapping ":state" to the value of the Launch status state and ":message" to the value of the Launch status message | ||
ExpressionAttributeValues: { | ||
":state": MOCK_LAUNCH.status.state, | ||
":message": MOCK_LAUNCH.status.message, | ||
}, | ||
} | ||
return this.dynamoDBClient.updateItem(updateParams) | ||
// Delete after retrieving the items | ||
Promise.all(completedEntries.map((entry) => this.deleteItem(entry))) | ||
return completedEntries | ||
} | ||
|
||
async deleteItem(message: SiteLaunchMessage): Promise<DeleteCommandOutput> { | ||
return this.dynamoDBClient.deleteItem(this.TABLE_NAME, { | ||
appId: MOCK_LAUNCH.appId, | ||
appId: message.appId, | ||
}) | ||
} | ||
} |
Oops, something went wrong.