diff --git a/package.json b/package.json index 8fccba4..6029b35 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "react-simple-code-editor": "^0.13.1", "sass": "^1.58.3", "tailwindcss": "^3.2.7", + "true-myth": "^5", "typescript": "^5.1.3", "winston": "^3.10.0", "winston-loki": "^6.0.7", diff --git a/src/location.ts b/src/location.ts index 3c14656..815781d 100644 --- a/src/location.ts +++ b/src/location.ts @@ -1,6 +1,8 @@ import { env } from '@/env/server.mjs'; // @ts-ignore import * as life360 from 'life360-node-api'; +import logger from '@/logger'; +import Result, { err, ok } from 'true-myth/result'; const center = { longitude: env.CENTER_LONGITUDE, @@ -53,42 +55,45 @@ export function distance( /** * @returns The distance in meters between me and the center */ -export async function getDistance(): Promise { +export async function getDistance(): Promise> { let client; try { // Set up the Life360 API client client = await life360.login(env.LIFE360_USERNAME, env.LIFE360_PASSWORD); } catch (e) { - throw new Error( - `Failed while logging in to Life360: ${ + return err( + `Failed while logging in to Life360 (${ e instanceof Error ? e.message : e - }` + })` ); } + // Get my current location let me; try { - // Get my current location const circles = await client.listCircles(); + // Assumed only in one circle (the first) const myCircle = circles[0]; + logger.debug(`${circles.length} circles found, using first.`, { + circle: myCircle + }); const members = await myCircle.listMembers(); + me = members.findById(env.LIFE360_MEMBER_ID); } catch (e) { - throw new Error( + return err( `Failed while getting my location from Life360: ${ e instanceof Error ? e.message : e }` ); } - // Parse my location into latitude and longitude + // Parse my latitude and longitude into floats const current = { latitude: parseFloat(me.location.latitude), longitude: parseFloat(me.location.longitude) }; - // Calculate the distance between my location and the center - const difference = distance(current, center, 'K') * 1000; - - return difference; + // Calculate the distance between my location and the center, multiply x1000 for meters + return ok(distance(current, center, 'K') * 1000); } diff --git a/yarn.lock b/yarn.lock index c2fdea9..6534dca 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1338,6 +1338,11 @@ triple-beam@^1.3.0: resolved "https://registry.yarnpkg.com/triple-beam/-/triple-beam-1.4.1.tgz#6fde70271dc6e5d73ca0c3b24e2d92afb7441984" integrity sha512-aZbgViZrg1QNcG+LULa7nhZpJTZSLm/mXnHXnbAbjmN5aSa0y7V+wvv6+4WaBtpISJzThKy+PIPxc1Nq1EJ9mg== +"true-myth@^5": + version "5.4.0" + resolved "https://registry.yarnpkg.com/true-myth/-/true-myth-5.4.0.tgz#eff4a4d09e47d616c8089d42c1e78ac9640c68a2" + integrity sha512-MonJqkJf+VOXZ2msbpvAI1uJWoyfCN7nirR8/fNK+IlFDWYNLb9iqeAc2uhIbAQBynHtM02azZgepQDQclOwAw== + ts-mixer@^6.0.2: version "6.0.3" resolved "https://registry.yarnpkg.com/ts-mixer/-/ts-mixer-6.0.3.tgz#69bd50f406ff39daa369885b16c77a6194c7cae6"