Skip to content

Commit

Permalink
Add night log endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
timbeccue committed Jun 28, 2022
1 parent 401d6ac commit 7e0d32a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
61 changes: 61 additions & 0 deletions api/night_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os
import json
import time
from http import HTTPStatus

from api.helpers import dynamodb_r
from api.helpers import _get_body
from api.helpers import http_response

NIGHT_LOG_TABLE = dynamodb_r.Table(os.getenv('NIGHT_LOG_TABLE'))

SECONDS_PER_DAY = 60 * 60 * 24

############################
###### Helpers ###########
############################

def get_note(site):
note = NIGHT_LOG_TABLE.get_item(
Key={ "site": site }
)
return note['Item']

def remove_note(site):
NIGHT_LOG_TABLE.delete_item(
Key={ "site": site}
)
return

def create_note(site, note_data):

# time to live timestamp two days from now
now = int(time.time())
ttl = now + (2 * SECONDS_PER_DAY)

response = NIGHT_LOG_TABLE.put_item(
Item={
"site": site,
"created_timestamp": now,
"ttl_timestamp_seconds": ttl,
"note_data": note_data
}
)
return response


############################
###### Handlers ###########
############################

def get_note_handler(event, context):
site = event['pathParameters']['site']
note = get_note(site)
return http_response(HTTPStatus.OK, note)

def create_note_handler(event, context):
body = _get_body(event)
site = event['pathParameters']['site']
note_data = body.get('note_data')
create_note(site, note_data)
return http_response(HTTPStatus.OK, 'Note created successfully')
Empty file.
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ env =
REMOTEHQ_ROOMS_TABLE=remotehq-control-rooms
CONFIG_TABLE_NAME=site_configurations
UPLOADS_LOG_TABLE_NAME=recent-uploads-log
NIGHT_LOG_TABLE=night-log
18 changes: 18 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ provider:
REGION: ${self:provider.region}
INFO_IMAGES_TABLE: 'info-images'
REMOTEHQ_ROOMS_TABLE: 'remotehq-control-rooms'
NIGHT_LOG_TABLE: 'night-log'
AUTH0_CLIENT_ID: ${file(./secrets.json):AUTH0_CLIENT_ID}
AUTH0_CLIENT_PUBLIC_KEY: ${file(./public_key)}

Expand Down Expand Up @@ -371,6 +372,23 @@ functions:
method: post
cors: true

### Night Log Endpoints
getNightLogNote:
handler: api/night_log.get_note_handler
events:
- http:
path: /nightlog/{site}
method: get
cors: true

createNightLogNote:
handler: api/night_log.create_note_handler
events:
- http:
path: /nightlog/{site}
method: post
cors: true

### Site Events Endpoints
siteEvents:
handler: api/events_handler.siteevents
Expand Down

0 comments on commit 7e0d32a

Please sign in to comment.