Skip to content

Commit 7e0d32a

Browse files
committed
Add night log endpoints
1 parent 401d6ac commit 7e0d32a

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

api/night_log.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import os
2+
import json
3+
import time
4+
from http import HTTPStatus
5+
6+
from api.helpers import dynamodb_r
7+
from api.helpers import _get_body
8+
from api.helpers import http_response
9+
10+
NIGHT_LOG_TABLE = dynamodb_r.Table(os.getenv('NIGHT_LOG_TABLE'))
11+
12+
SECONDS_PER_DAY = 60 * 60 * 24
13+
14+
############################
15+
###### Helpers ###########
16+
############################
17+
18+
def get_note(site):
19+
note = NIGHT_LOG_TABLE.get_item(
20+
Key={ "site": site }
21+
)
22+
return note['Item']
23+
24+
def remove_note(site):
25+
NIGHT_LOG_TABLE.delete_item(
26+
Key={ "site": site}
27+
)
28+
return
29+
30+
def create_note(site, note_data):
31+
32+
# time to live timestamp two days from now
33+
now = int(time.time())
34+
ttl = now + (2 * SECONDS_PER_DAY)
35+
36+
response = NIGHT_LOG_TABLE.put_item(
37+
Item={
38+
"site": site,
39+
"created_timestamp": now,
40+
"ttl_timestamp_seconds": ttl,
41+
"note_data": note_data
42+
}
43+
)
44+
return response
45+
46+
47+
############################
48+
###### Handlers ###########
49+
############################
50+
51+
def get_note_handler(event, context):
52+
site = event['pathParameters']['site']
53+
note = get_note(site)
54+
return http_response(HTTPStatus.OK, note)
55+
56+
def create_note_handler(event, context):
57+
body = _get_body(event)
58+
site = event['pathParameters']['site']
59+
note_data = body.get('note_data')
60+
create_note(site, note_data)
61+
return http_response(HTTPStatus.OK, 'Note created successfully')

api/tests/request_events/all_config.json

Whitespace-only changes.

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ env =
99
REMOTEHQ_ROOMS_TABLE=remotehq-control-rooms
1010
CONFIG_TABLE_NAME=site_configurations
1111
UPLOADS_LOG_TABLE_NAME=recent-uploads-log
12+
NIGHT_LOG_TABLE=night-log

serverless.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ provider:
5959
REGION: ${self:provider.region}
6060
INFO_IMAGES_TABLE: 'info-images'
6161
REMOTEHQ_ROOMS_TABLE: 'remotehq-control-rooms'
62+
NIGHT_LOG_TABLE: 'night-log'
6263
AUTH0_CLIENT_ID: ${file(./secrets.json):AUTH0_CLIENT_ID}
6364
AUTH0_CLIENT_PUBLIC_KEY: ${file(./public_key)}
6465

@@ -371,6 +372,23 @@ functions:
371372
method: post
372373
cors: true
373374

375+
### Night Log Endpoints
376+
getNightLogNote:
377+
handler: api/night_log.get_note_handler
378+
events:
379+
- http:
380+
path: /nightlog/{site}
381+
method: get
382+
cors: true
383+
384+
createNightLogNote:
385+
handler: api/night_log.create_note_handler
386+
events:
387+
- http:
388+
path: /nightlog/{site}
389+
method: post
390+
cors: true
391+
374392
### Site Events Endpoints
375393
siteEvents:
376394
handler: api/events_handler.siteevents

0 commit comments

Comments
 (0)