diff --git a/api/night_log.py b/api/night_log.py new file mode 100644 index 0000000..83cc203 --- /dev/null +++ b/api/night_log.py @@ -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') diff --git a/api/tests/request_events/all_config.json b/api/tests/request_events/all_config.json deleted file mode 100644 index e69de29..0000000 diff --git a/pytest.ini b/pytest.ini index d72976c..d61b2bb 100644 --- a/pytest.ini +++ b/pytest.ini @@ -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 diff --git a/serverless.yml b/serverless.yml index ba00c55..e457697 100644 --- a/serverless.yml +++ b/serverless.yml @@ -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)} @@ -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