diff --git a/apps/backend/src/dynamodb.ts b/apps/backend/src/dynamodb.ts index ff92a2b..ad1bf9f 100644 --- a/apps/backend/src/dynamodb.ts +++ b/apps/backend/src/dynamodb.ts @@ -175,6 +175,38 @@ export class DynamoDbService { return result.Attributes; } + public async updateField( + tableName: string, + key: { [key: string]: any }, + field: string, + value: string, + ): Promise { + const params = { + TableName: tableName, + Key: key, + UpdateExpression: `SET #field = :value`, + ExpressionAttributeNames: { + '#field': field, + }, + ExpressionAttributeValues: { + ':value': { S: value }, + }, + ReturnValue: 'ALL_NEW', + }; + console.log(key); + console.log(params) + try { + const command = new UpdateItemCommand(params); + await this.dynamoDbClient.send(command); + const result = await this.getItem(tableName, key); + console.log(result); + return result; + } catch (error) { + console.error('DynamoDB UpdateItem Error:', error); + throw new Error(`Unable to update item in ${tableName}`); + } + } + public async updateItemWithExpression( tableName: string, key: { [key: string]: any }, diff --git a/apps/backend/src/site/site.controller.ts b/apps/backend/src/site/site.controller.ts index 4848ea0..ec01b3c 100644 --- a/apps/backend/src/site/site.controller.ts +++ b/apps/backend/src/site/site.controller.ts @@ -3,6 +3,7 @@ import { Delete, Get, Post, + Put, Body, Param, Query @@ -57,6 +58,10 @@ export class SiteController { return this.siteService.deleteSite(siteId); } + @Put("/adopt/:id") + public async setSiteStatusAdopt(@Param("id") siteId: number): Promise { + return this.siteService.adoptSite(siteId); + } diff --git a/apps/backend/src/site/site.service.ts b/apps/backend/src/site/site.service.ts index e82fe56..37021f6 100644 --- a/apps/backend/src/site/site.service.ts +++ b/apps/backend/src/site/site.service.ts @@ -93,12 +93,20 @@ export class SiteService { } } + public async adoptSite(siteId: number): Promise { + try { + const key = { 'siteId': { S: siteId.toString() } }; + const result = await this.dynamoDbService.updateField(this.tableName, key, "siteStatus", "Adopted") + } catch (e) { + throw new Error("Unable to set site status to Adopted:" + e); + } + } private mapDynamoDBItemToSite = (objectId: number, item: { [key: string]: any }): SiteModel => { return { siteID: objectId, siteName: item["siteName"].S, - siteStatus: SiteStatus.AVAILABLE, //placeholder until table is updated + siteStatus: item["siteStatus"].S, assetType: item["assetType"].S, symbolType: item["symbolType"].S, siteLatitude: item["siteLatitude"].S,