|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: MIT-0 |
| 3 | +import { marshall, unmarshall } from '@aws-sdk/util-dynamodb'; |
| 4 | +import { DynamoDBClient, GetItemCommand, GetItemCommandInput, PutItemCommand, PutItemCommandInput, PutItemCommandOutput, UpdateItemCommand, UpdateItemCommandInput, UpdateItemCommandOutput } from '@aws-sdk/client-dynamodb'; |
| 5 | +import { EventBridgeClient, PutEventsCommand, PutEventsCommandInput, PutEventsCommandOutput, PutEventsRequestEntry } from '@aws-sdk/client-eventbridge'; |
| 6 | + |
| 7 | +// Empty configuration for DynamoDB |
| 8 | +const ddbClient = new DynamoDBClient({}); |
| 9 | +const DDB_TABLE = process.env.DYNAMODB_TABLE; |
| 10 | + |
| 11 | +// Empty configuration for EventBridge |
| 12 | +const eventsClient = new EventBridgeClient({}); |
| 13 | +const EVENT_BUS = process.env.EVENT_BUS; |
| 14 | + |
| 15 | +// Internal data model |
| 16 | +const fields: string[] = [ |
| 17 | + "address", |
| 18 | + "property_id", |
| 19 | + "seller_name" |
| 20 | +]; |
| 21 | + |
| 22 | +// External data types |
| 23 | +export const ContractCreatedMetric: string = "ContractCreated"; |
| 24 | +export const ContractUpdatedMetric: string = "ContractUpdated"; |
| 25 | +export const ContractEventMetric: string = "ContractEvent"; |
| 26 | + |
| 27 | +export type ContractStatusChangedEvent = { |
| 28 | + contract_last_modified_on: string; |
| 29 | + contract_id: string; |
| 30 | + property_id: string; |
| 31 | + contract_status: ContractStatusEnum; |
| 32 | +} |
| 33 | + |
| 34 | +export type ContractDBType = { |
| 35 | + address?: string; |
| 36 | + property_id: string; |
| 37 | + contract_id?: string; |
| 38 | + seller_name?: string; |
| 39 | + contract_status: ContractStatusEnum; |
| 40 | + contract_created?: string; |
| 41 | + contract_last_modified_on?: string; |
| 42 | +}; |
| 43 | + |
| 44 | +export enum ContractStatusEnum { |
| 45 | + DRAFT = 'DRAFT', |
| 46 | + APPROVED = 'APPROVED', |
| 47 | +}; |
| 48 | + |
| 49 | +export interface ContractError extends Error { |
| 50 | + propertyId: string; |
| 51 | + object?: any; |
| 52 | +} |
| 53 | + |
| 54 | +export interface ContractResponse { |
| 55 | + propertyId: string; |
| 56 | + metadata: any; |
| 57 | +} |
| 58 | + |
| 59 | +export function validData(data: any): boolean { |
| 60 | + for (let i = 0; i < fields.length; i++) { |
| 61 | + let field = fields[i]; |
| 62 | + if (!(field in data)) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + } |
| 66 | + return true; |
| 67 | +} |
| 68 | + |
| 69 | +export async function getContractFor(propertyId: string): Promise<ContractDBType | undefined> { |
| 70 | + const getItemCommandInput: GetItemCommandInput = { |
| 71 | + Key: {'property_id': {S: propertyId}}, |
| 72 | + ProjectionExpression: 'contract_id, property_id, contract_status, address, seller_name, contract_created, contract_last_modified_on', |
| 73 | + TableName: DDB_TABLE |
| 74 | + }; |
| 75 | + |
| 76 | + const data = await ddbClient.send(new GetItemCommand(getItemCommandInput)); |
| 77 | + if (data.Item === undefined) { |
| 78 | + return undefined; |
| 79 | + } else { |
| 80 | + const result: ContractDBType = (unmarshall(data.Item) as ContractDBType); |
| 81 | + return result; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +export async function updateEntryInDB(dbEntry: ContractDBType): Promise<ContractResponse> { |
| 86 | + // Build the Command objects |
| 87 | + const ddbUpdateCommandInput: UpdateItemCommandInput = { |
| 88 | + TableName: DDB_TABLE, |
| 89 | + Key: {property_id: {S: dbEntry.property_id}}, |
| 90 | + UpdateExpression: 'set contract_status = :t, modified_date = :m', |
| 91 | + ExpressionAttributeValues: { |
| 92 | + ':t': {S: (dbEntry.contract_status as string)}, |
| 93 | + ':m': {S: (dbEntry.contract_last_modified_on as string)} |
| 94 | + } |
| 95 | + }; |
| 96 | + const ddbUpdateCommand = new UpdateItemCommand(ddbUpdateCommandInput); |
| 97 | + |
| 98 | + // Send the command |
| 99 | + const ddbUpdateCommandOutput: UpdateItemCommandOutput = await ddbClient.send(ddbUpdateCommand); |
| 100 | + if (ddbUpdateCommandOutput.$metadata.httpStatusCode != 200) { |
| 101 | + const error: ContractError = { |
| 102 | + propertyId: dbEntry.property_id, |
| 103 | + name: "ContractDBUpdateError", |
| 104 | + message: "Response error code: " + ddbUpdateCommandOutput.$metadata.httpStatusCode, |
| 105 | + object: ddbUpdateCommandOutput.$metadata |
| 106 | + }; |
| 107 | + throw error; |
| 108 | + } |
| 109 | + |
| 110 | + const response: ContractResponse = { |
| 111 | + propertyId: dbEntry.property_id, |
| 112 | + metadata: ddbUpdateCommandOutput.$metadata |
| 113 | + } |
| 114 | + return response; |
| 115 | +} |
| 116 | + |
| 117 | +export async function saveEntryToDB(dbEntry: ContractDBType): Promise<ContractResponse> { |
| 118 | + // Build the Command objects |
| 119 | + const ddbPutCommandInput: PutItemCommandInput = { |
| 120 | + TableName: DDB_TABLE, |
| 121 | + Item: marshall(dbEntry, {removeUndefinedValues: true}) |
| 122 | + }; |
| 123 | + const ddbPutCommand = new PutItemCommand(ddbPutCommandInput); |
| 124 | + |
| 125 | + // Send the command |
| 126 | + const ddbPutCommandOutput: PutItemCommandOutput = await ddbClient.send(ddbPutCommand); |
| 127 | + if (ddbPutCommandOutput.$metadata.httpStatusCode != 200) { |
| 128 | + let error: ContractError = { |
| 129 | + propertyId: dbEntry.property_id, |
| 130 | + name: "ContractDBSaveError", |
| 131 | + message: "Response error code: " + ddbPutCommandOutput.$metadata.httpStatusCode, |
| 132 | + object: ddbPutCommandOutput.$metadata |
| 133 | + }; |
| 134 | + throw error; |
| 135 | + } |
| 136 | + |
| 137 | + let response: ContractResponse = { |
| 138 | + propertyId: dbEntry.property_id, |
| 139 | + metadata: ddbPutCommandOutput.$metadata |
| 140 | + } |
| 141 | + return response; |
| 142 | +} |
| 143 | + |
| 144 | +export async function fireContractEvent(eventDetail: ContractStatusChangedEvent, source: string, detailType: string): Promise<ContractResponse> { |
| 145 | + const propertyId = eventDetail.property_id; |
| 146 | + |
| 147 | + // Build the Command objects |
| 148 | + const eventsPutEventsCommandInputEntry: PutEventsRequestEntry = { |
| 149 | + EventBusName: EVENT_BUS, |
| 150 | + Time: new Date(), |
| 151 | + Source: source, |
| 152 | + DetailType: detailType, |
| 153 | + Detail: JSON.stringify(eventDetail) |
| 154 | + }; |
| 155 | + const eventsPutEventsCommandInput: PutEventsCommandInput = { |
| 156 | + Entries: [ |
| 157 | + eventsPutEventsCommandInputEntry |
| 158 | + ] |
| 159 | + }; |
| 160 | + const eventsPutEventsCommand = new PutEventsCommand(eventsPutEventsCommandInput); |
| 161 | + |
| 162 | + // Send the command |
| 163 | + const eventsPutEventsCommandOutput: PutEventsCommandOutput = await eventsClient.send(eventsPutEventsCommand); |
| 164 | + |
| 165 | + if (eventsPutEventsCommandOutput.$metadata.httpStatusCode != 200) { |
| 166 | + let error: ContractError = { |
| 167 | + propertyId: propertyId, |
| 168 | + name: "ContractEventsError", |
| 169 | + message: "Response invalid: " + eventsPutEventsCommandOutput.$metadata.httpStatusCode, |
| 170 | + object: eventsPutEventsCommandOutput.$metadata |
| 171 | + }; |
| 172 | + throw error; |
| 173 | + |
| 174 | + } |
| 175 | + let response: ContractResponse = { |
| 176 | + propertyId: propertyId, |
| 177 | + metadata: eventsPutEventsCommandOutput.$metadata |
| 178 | + } |
| 179 | + return response; |
| 180 | +} |
0 commit comments