Skip to content

Commit

Permalink
feat: registered sessions now are favorited. Added additional data to…
Browse files Browse the repository at this point in the history
… session regi #78
  • Loading branch information
rbento1096 committed Mar 16, 2024
1 parent 4d8bcd1 commit cac7d00
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
37 changes: 31 additions & 6 deletions back-end/src/handlers/registrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import { User } from '../models/user.model';
const DDB_TABLES = {
users: process.env.DDB_TABLE_users,
sessions: process.env.DDB_TABLE_sessions,
registrations: process.env.DDB_TABLE_registrations
registrations: process.env.DDB_TABLE_registrations,
usersFavoriteSessions: process.env.DDB_TABLE_usersFavoriteSessions
};

const ddb = new DynamoDB();
Expand All @@ -35,6 +36,10 @@ class SessionRegistrations extends ResourceController {
}

protected async checkAuthBeforeRequest(): Promise<void> {
// NOTE: DONT DO THIS CHECK HERE, DO IT ON THE DELETE/POST
// this.app.configurations.areSessionRegistrationsOpen // @todo use this in the code (and in back-end as well!!)


try {
this.user = new User(await ddb.get({ TableName: DDB_TABLES.users, Key: { userId: this.principalId } }));
} catch (err) {
Expand Down Expand Up @@ -72,13 +77,15 @@ class SessionRegistrations extends ResourceController {
}
}

protected async postResources(): Promise<any> {
protected async postResource(): Promise<any> {
// @todo configurations.canSignUpForSessions()

this.registration = new SessionRegistration({
sessionId: this.resourceId,
userId: this.principalId,
registrationDateInMs: new Date().getTime()
userId: this.user.userId,
registrationDateInMs: new Date().getTime(),
name: this.user.getName(),
esnCountry: this.user.sectionCountry
});

return await this.putSafeResource();
Expand All @@ -105,7 +112,16 @@ class SessionRegistrations extends ResourceController {
}
};

await ddb.transactWrites([{ Delete: deleteSessionRegistration }, { Update: updateSessionCount }]);
const removeFromFavorites = {
TableName: DDB_TABLES.usersFavoriteSessions,
Key: { userId: this.principalId, sessionId }
};

await ddb.transactWrites([
{ Delete: deleteSessionRegistration },
{ Delete: removeFromFavorites },
{ Update: updateSessionCount }
]);
} catch (err) {
throw new HandledError('Delete failed');
}
Expand All @@ -129,7 +145,16 @@ class SessionRegistrations extends ResourceController {
}
};

await ddb.transactWrites([{ Put: putSessionRegistration }, { Update: updateSessionCount }]);
const addToFavorites = {
TableName: DDB_TABLES.usersFavoriteSessions,
Item: { userId: this.principalId, sessionId: this.resourceId }
}

await ddb.transactWrites([
{ Put: putSessionRegistration },
{ Put: addToFavorites },
{ Update: updateSessionCount }
]);

return this.registration;
} catch (err) {
Expand Down
10 changes: 10 additions & 0 deletions back-end/src/models/sessionRegistration.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ export class SessionRegistration extends Resource {
* The date of the registration.
*/
registrationDateInMs: number;
/**
* The user's name.
*/
name: string;
/**
* The user's ESN Country if any.
*/
esnCountry?: string;

load(x: any): void {
super.load(x);
this.sessionId = this.clean(x.sessionId, String);
this.userId = this.clean(x.userId, String);
this.registrationDateInMs = this.clean(x.registrationDateInMs, t => new Date(t).getTime());
this.name = this.clean(x.name, String);
this.esnCountry = this.clean(x.esnCountry, String);
}
}

0 comments on commit cac7d00

Please sign in to comment.