Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt sites endpoint #58

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions apps/backend/src/dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,38 @@ export class DynamoDbService {
return result.Attributes;
}

public async updateField(
tableName: string,
key: { [key: string]: any },
field: string,
value: string,
): Promise<any> {
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 },
Expand Down
5 changes: 5 additions & 0 deletions apps/backend/src/site/site.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Delete,
Get,
Post,
Put,
Body,
Param,
Query
Expand Down Expand Up @@ -57,6 +58,10 @@ export class SiteController {
return this.siteService.deleteSite(siteId);
}

@Put("/adopt/:id")
public async setSiteStatusAdopt(@Param("id") siteId: number): Promise<void> {
return this.siteService.adoptSite(siteId);
}



Expand Down
10 changes: 9 additions & 1 deletion apps/backend/src/site/site.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,20 @@ export class SiteService {
}
}

public async adoptSite(siteId: number): Promise<void> {
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,
Expand Down