-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rbento1096
committed
Feb 17, 2024
1 parent
5727372
commit 836c263
Showing
4 changed files
with
302 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/// | ||
/// IMPORTS | ||
/// | ||
|
||
import { DynamoDB, RCError, ResourceController } from 'idea-aws'; | ||
|
||
import { Room } from '../models/room.model'; | ||
import { VenueLinked } from '../models/venue.model'; | ||
import { User } from '../models/user.model'; | ||
|
||
/// | ||
/// CONSTANTS, ENVIRONMENT VARIABLES, HANDLER | ||
/// | ||
|
||
const PROJECT = process.env.PROJECT; | ||
|
||
const DDB_TABLES = { | ||
users: process.env.DDB_TABLE_users, | ||
rooms: process.env.DDB_TABLE_rooms, | ||
venues: process.env.DDB_TABLE_venues | ||
}; | ||
|
||
const ddb = new DynamoDB(); | ||
|
||
export const handler = (ev: any, _: any, cb: any) => new Rooms(ev, cb).handleRequest(); | ||
|
||
/// | ||
/// RESOURCE CONTROLLER | ||
/// | ||
|
||
class Rooms extends ResourceController { | ||
user: User; | ||
room: Room; | ||
|
||
constructor(event: any, callback: any) { | ||
super(event, callback, { resourceId: 'roomId' }); | ||
} | ||
|
||
protected async checkAuthBeforeRequest(): Promise<void> { | ||
try { | ||
this.user = new User(await ddb.get({ TableName: DDB_TABLES.users, Key: { userId: this.principalId } })); | ||
} catch (err) { | ||
throw new RCError('User not found'); | ||
} | ||
|
||
if (!this.resourceId) return; | ||
|
||
try { | ||
this.room = new Room(await ddb.get({ TableName: DDB_TABLES.rooms, Key: { roomId: this.resourceId } })); | ||
} catch (err) { | ||
throw new RCError('Room not found'); | ||
} | ||
} | ||
|
||
protected async getResource(): Promise<Room> { | ||
return this.room; | ||
} | ||
|
||
protected async putResource(): Promise<Room> { | ||
if (!this.user.permissions.canManageContents) throw new RCError('Unauthorized'); | ||
|
||
const oldResource = new Room(this.room); | ||
this.room.safeLoad(this.body, oldResource); | ||
|
||
return await this.putSafeResource(); | ||
} | ||
private async putSafeResource(opts: { noOverwrite?: boolean } = {}): Promise<Room> { | ||
const errors = this.room.validate(); | ||
if (errors.length) throw new RCError(`Invalid fields: ${errors.join(', ')}`); | ||
|
||
this.room.venue = new VenueLinked( | ||
await ddb.get({ TableName: DDB_TABLES.venues, Key: { venueId: this.room.venue.venueId } }) | ||
); | ||
|
||
try { | ||
const putParams: any = { TableName: DDB_TABLES.rooms, Item: this.room }; | ||
if (opts.noOverwrite) putParams.ConditionExpression = 'attribute_not_exists(roomId)'; | ||
await ddb.put(putParams); | ||
|
||
return this.room; | ||
} catch (err) { | ||
throw new RCError('Operation failed'); | ||
} | ||
} | ||
|
||
protected async deleteResource(): Promise<void> { | ||
if (!this.user.permissions.canManageContents) throw new RCError('Unauthorized'); | ||
|
||
try { | ||
await ddb.delete({ TableName: DDB_TABLES.rooms, Key: { roomId: this.resourceId } }); | ||
} catch (err) { | ||
throw new RCError('Delete failed'); | ||
} | ||
} | ||
|
||
protected async postResources(): Promise<Room> { | ||
if (!this.user.permissions.canManageContents) throw new RCError('Unauthorized'); | ||
|
||
this.room = new Room(this.body); | ||
this.room.roomId = await ddb.IUNID(PROJECT); | ||
|
||
return await this.putSafeResource({ noOverwrite: true }); | ||
} | ||
|
||
protected async getResources(): Promise<Room[]> { | ||
try { | ||
const rooms = (await ddb.scan({ TableName: DDB_TABLES.rooms })).map((x: Room) => new Room(x)); | ||
|
||
const filteredRooms = this.queryParams.venue | ||
? rooms.filter(x => x.venue.venueId === this.queryParams.venue) | ||
: rooms; | ||
|
||
const sortedRooms = filteredRooms.sort((a, b) => a.name.localeCompare(b.name)); | ||
|
||
return sortedRooms; | ||
} catch (err) { | ||
throw new RCError('Operation failed'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { isEmpty, Resource } from 'idea-toolbox'; | ||
|
||
import { VenueLinked } from './venue.model'; | ||
|
||
export class Room extends Resource { | ||
/** | ||
* The room ID. | ||
*/ | ||
roomId: string; | ||
/** | ||
* The name of the room. | ||
*/ | ||
name: string; | ||
/** | ||
* The venue where this room is located | ||
*/ | ||
venue: VenueLinked; | ||
/** | ||
* The room's internal location. | ||
*/ | ||
internalLocation: string; | ||
/** | ||
* A description for the room. | ||
*/ | ||
description: string; | ||
/** | ||
* An URI for an image of the room. | ||
*/ | ||
imageURI: string; | ||
/** | ||
* An URI for a plan of the room. | ||
*/ | ||
planImageURI: string; | ||
|
||
load(x: any): void { | ||
super.load(x); | ||
this.roomId = this.clean(x.roomId, String); | ||
this.name = this.clean(x.name, String); | ||
this.venue = typeof x.venue === 'string' ? new VenueLinked({ venueId: x.venue }) : new VenueLinked(x.venue); | ||
this.internalLocation = this.clean(x.internalLocation, String); | ||
this.description = this.clean(x.description, String); | ||
this.imageURI = this.clean(x.imageURI, String); | ||
this.planImageURI = this.clean(x.planImageURI, String); | ||
} | ||
safeLoad(newData: any, safeData: any): void { | ||
super.safeLoad(newData, safeData); | ||
this.roomId = safeData.roomId; | ||
} | ||
validate(): string[] { | ||
const e = super.validate(); | ||
if (isEmpty(this.name)) e.push('name'); | ||
if (!this.venue.venueId) e.push('venue'); | ||
return e; | ||
} | ||
} | ||
|
||
export class RoomLinked extends Resource { | ||
roomId: string; | ||
name: string; | ||
venue: VenueLinked; | ||
|
||
load(x: any): void { | ||
super.load(x); | ||
this.roomId = this.clean(x.roomId, String); | ||
this.name = this.clean(x.name, String); | ||
this.venue = new VenueLinked(x.venue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters