diff --git a/providers/tesla/tesla-api.ts b/providers/tesla/tesla-api.ts index 1234cb0e..baca74ab 100644 --- a/providers/tesla/tesla-api.ts +++ b/providers/tesla/tesla-api.ts @@ -91,6 +91,7 @@ export class TeslaAPI { formData.append("refresh_token", refresh_token); const authResponse = (await this.authAPI.post("/oauth2/v3/token", formData.toString())) as any; + log(LogLevel.Trace, `renewToken response: ${JSON.stringify(authResponse)}`); return this.parseTokenResponse(authResponse); } catch (e) { console.debug(`TeslaAPI.renewToken error: ${e}`); diff --git a/providers/tesla/tesla-server.ts b/providers/tesla/tesla-server.ts index 189c5d40..0cb477b3 100644 --- a/providers/tesla/tesla-server.ts +++ b/providers/tesla/tesla-server.ts @@ -46,6 +46,15 @@ export async function maintainToken( log(LogLevel.Trace, `Token ${token.access_token} is still valid`); return token as TeslaToken; } + + await db.pg.none( + `UPDATE service_provider SET service_data = jsonb_strip_nulls(service_data || $2) WHERE service_data @> $1;`, + [ + { token: { refresh_token: token.refresh_token } }, + { renewing_token: true }, + ] + ); + log(LogLevel.Trace, `Token ${token.access_token} is invalid, calling renewToken`); const newToken = await teslaAPI.renewToken(token.refresh_token); validToken(db, token.refresh_token, newToken); @@ -70,7 +79,7 @@ async function validToken( `UPDATE service_provider SET service_data = jsonb_strip_nulls(service_data || $2) WHERE service_data @> $1 RETURNING *;`, [ { token: { refresh_token: oldRefreshToken } }, - { updated: Date.now(), token: newToken, invalid_token: null }, + { updated: Date.now(), token: newToken, invalid_token: null, renewing_token: null }, ] ); for (const s of dblist) {