Skip to content

Commit

Permalink
Merge pull request #59 from Code-4-Community/gi-43-post-application-f…
Browse files Browse the repository at this point in the history
…inal

GI-43 POST NEW APPLICATION
  • Loading branch information
mahekagg authored Dec 5, 2024
2 parents c357d8d + bcfd4e7 commit 942649a
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 9 deletions.
10 changes: 9 additions & 1 deletion apps/backend/src/applications/applications.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Controller, Get, Put, Param, Query } from '@nestjs/common';
import { Controller, Get, Put, Post, Body, Param, Query } from '@nestjs/common';
import { ApplicationsService } from './applications.service';
import { ApplicationsModel } from './applications.model';
import { ApplicationStatus } from './applications.model';
import { NewApplicationInput } from '../dtos/newApplicationsDTO';


@Controller('applications')
export class ApplicationsController {
Expand Down Expand Up @@ -36,4 +38,10 @@ export class ApplicationsController {

return this.applicationsService.updateApplicationStatus(appId, appStatus);
}

@Post()
public async postApplication(@Body() applicationData: NewApplicationInput) {
return this.applicationsService.postApplication(applicationData);
}

}
17 changes: 10 additions & 7 deletions apps/backend/src/applications/applications.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ export type ApplicationsModel = {
};

export type ApplicationInputModel = {
appId: {S: string},
userId: {S: string},
siteId: {S: string},
names: {S: string},
status: {S: string},
dateApplied: {S: string},
isFirstApplication: {S: string},
appId: {
N: string
},
userId: { N: string },
siteId: { N: string },
names: { SS: string[] }, // For an array of strings, use SS (String Set) in DynamoDB
status: { S: string },
dateApplied: { S: string }, // Date should be formatted as a string, typically ISO string
isFirstApplication: { S: string },
};



export enum ApplicationStatus {
APPROVED = 'Approved',
PENDING = 'Pending',
Expand Down
38 changes: 37 additions & 1 deletion apps/backend/src/applications/applications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import { ApplicationInputModel, ApplicationsModel } from './applications.model';
import { DynamoDbService } from '../dynamodb';
import { ApplicationStatus } from './applications.model';
// import { NewApplicationInput } from '../dtos/newApplicationsDTO';
import { NewApplicationInput } from '../dtos/newApplicationsDTO';

@Injectable()
export class ApplicationsService {
Expand Down Expand Up @@ -68,6 +68,40 @@ export class ApplicationsService {
throw new Error('Unable to update application status: ' + e);
}
}

public async postApplication(applicationData: NewApplicationInput) {
const applicationModel = this.PostInputToApplicationModel(applicationData);
console.log("Received application data:", applicationData);

const newId = await this.dynamoDbService.getHighestAppId(this.tableName) + 1;

applicationModel.appId.N = newId.toString();
console.log("Using new ID:" + applicationModel.appId.N)
try {
const result = await this.dynamoDbService.postItem(this.tableName, applicationModel);
return {...result, newApplicationId: newId.toString()};
} catch (e) {
throw new Error("Unable to post new application: " + e);
}
}


private PostInputToApplicationModel = (input: NewApplicationInput): ApplicationInputModel => {

return {
appId: { N: input.appId.toString() },
userId: { N: input.userId.toString() },
siteId: { N: input.siteId.toString() },
names: { SS: input.names },
status: { S: input.status as ApplicationStatus},
dateApplied: { S: input.dateApplied},
isFirstApplication: { S: input.isFirstApplication.toString() },
};
};





private mapDynamoDBItemToApplication = (item: {
[key: string]: any;

Check warning on line 107 in apps/backend/src/applications/applications.service.ts

View workflow job for this annotation

GitHub Actions / pre-deploy

Unexpected any. Specify a different type
Expand All @@ -83,3 +117,5 @@ export class ApplicationsService {
};
};
}


16 changes: 16 additions & 0 deletions apps/backend/src/dtos/newApplicationsDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type NewApplicationInput = {
appId: number; // Optional if auto-generated
userId: number;
siteId: number;
names: string[]; // Array with empty string by default if not provided
status: ApplicationStatus; // Defaults to "PENDING"
dateApplied: string; // Defaults to an ISO string if not provided
isFirstApplication: boolean;
};

export enum ApplicationStatus {
APPROVED = 'Approved',
PENDING = 'Pending',
DENIED = 'Denied',
}

32 changes: 32 additions & 0 deletions apps/backend/src/dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,38 @@ export class DynamoDbService {
}
}

public async getHighestAppId(
tableName: string,
): Promise<number | undefined> {
const params: any = {
TableName: tableName,
ProjectionExpression: 'appId', // Project only the appID attribute
};

try {
const data = await this.dynamoDbClient.send(new ScanCommand(params));
console.log(data);
const appIds = data.Items.map((item) => parseInt(item.appId.N, 10)); // Convert to numbers
console.log("Scanned Items:", data.Items);

console.log(appIds);
// Handle potential parsing errors
const validAppIds = appIds.filter((id) => !isNaN(id));

if (validAppIds.length === 0) {
return undefined; // No valid apps IDs found
}

const highestAppId = validAppIds.reduce((max, current) =>
Math.max(max, current),
);
return highestAppId;
} catch (error) {
console.error('DynamoDB Scan Error:', error);
throw new Error(`Unable to scan table ${tableName}`);
}
}

public async getItem(
tableName: string,
key: { [key: string]: any },
Expand Down

0 comments on commit 942649a

Please sign in to comment.