Skip to content

Commit a1657ee

Browse files
committed
Send notification in the background
1 parent e10f306 commit a1657ee

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/controller.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from "node:assert/strict";
12
import moment from "moment-timezone";
23
import { successResponse } from "./apiUtils";
34
import { env } from "./config";
@@ -41,21 +42,21 @@ export const logSleepRoute = async (req: Request) => {
4142
throw new ApiError("Failed to append rows to Google Sheet", error);
4243
});
4344

44-
const updatedRows = await getObjectArrayHeader(
45+
const updatedRowsResponse = await getObjectArrayHeader(
4546
sheetsObj,
4647
env.SPREADSHEET_ID,
4748
result.updatedRange
4849
).catch((error) => {
4950
throw new ApiError("Failed to retrieve row after writing", error);
5051
});
5152

52-
const response = {
53-
updatedRow: updatedRows[0] as SheetsSleepEntry,
54-
};
53+
const updatedRows = SheetsSleepEntry.array().parse(updatedRowsResponse);
54+
const updatedRow = updatedRows.at(0);
55+
assert(updatedRow, "Updated row should be present");
5556

56-
await sendEntryNotification(response.updatedRow);
57+
sendEntryNotification(updatedRow);
5758

58-
return successResponse(response, "Successfully added sleep entry");
59+
return successResponse({ updatedRow }, "Successfully added sleep entry");
5960
};
6061

6162
export const getSleepRoute = async () => {
@@ -121,7 +122,7 @@ export const replaceLastSleepRoute = async (req: Request) => {
121122

122123
const rangeToUpdate = getLastRowRange(rows);
123124

124-
const result: GoogleSheetsAppendUpdates = await update(
125+
const result = await update(
125126
sheetsObj,
126127
env.SPREADSHEET_ID,
127128
rangeToUpdate,
@@ -130,21 +131,26 @@ export const replaceLastSleepRoute = async (req: Request) => {
130131
throw new ApiError("Failed to update rows", error);
131132
});
132133

133-
const updatedRows = await getObjectArrayHeader(
134+
assert(result.updatedRange, "Updated range should be present");
135+
136+
const updatedRowsResponse = await getObjectArrayHeader(
134137
sheetsObj,
135138
env.SPREADSHEET_ID,
136139
result.updatedRange
137140
).catch((error) => {
138141
throw new ApiError("Failed to retrieve row after updating", error);
139142
});
140143

141-
const response = {
142-
updatedRow: updatedRows[0] as SheetsSleepEntry,
143-
};
144+
const updatedRows = SheetsSleepEntry.array().parse(updatedRowsResponse);
145+
const updatedRow = updatedRows.at(0);
146+
assert(updatedRow, "Updated row should be present");
144147

145-
await sendEntryNotification(response.updatedRow);
148+
sendEntryNotification(updatedRow);
146149

147-
return successResponse(response, "Successfully replaced last sleep entry");
150+
return successResponse(
151+
{ updatedRow },
152+
"Successfully replaced last sleep entry"
153+
);
148154
};
149155

150156
const getSleepEntryFromGeolocationPosition = (

0 commit comments

Comments
 (0)