import type { ApiClient } from '../../core/ApiClient' import type { Account } from '../accounts/types' import type { User } from '../users/types' import type { RegisterTokenPayload, SetOtpSecretKeyPayload, TakeoverPayload, UpdateMeAccountPayload, UpdateMePayload, UpdateMeStatusPayload, } from './types' export class MeApi { protected readonly client: ApiClient constructor(client: ApiClient) { this.client = client } /** * Returns the currently authenticated user, including their roles, departments, and timetable. * @permissions users.view */ getMe(headers?: Record): Promise { return this.client.get('/me', headers) } /** Returns the account associated with the currently authenticated user. */ getAccount(headers?: Record): Promise { return this.client.get('/me/account', headers) } /** * Returns credit and usage amounts for the current account. * Excludes Digisac's own internal credit amounts. */ getAccountAmounts(headers?: Record): Promise> { return this.client.get>('/me/get-account-amounts', headers) } /** * Returns a URL to the Agnus billing/plan management page for the current account. * @permissions myAccount.myPlan */ getAgnusMyPlanUrl(headers?: Record): Promise<{ url: string }> { return this.client.get<{ url: string }>('/me/account/agnus-my-plan-url', headers) } /** * Updates the current user's profile fields such as name, language, and preferences. * If a new password is provided, it is validated against the last 6 password history entries * and all existing access tokens for the user are revoked. */ updateMe(body: UpdateMePayload, headers?: Record): Promise { return this.client.put('/me', body, headers) } /** Updates account-level settings such as name, settings object, wizard progress, and default department. */ updateAccount(body: UpdateMeAccountPayload, headers?: Record): Promise { return this.client.put('/me/account', body, headers) } /** * Updates the presence status of the current user (`online`, `offline`, or `away`). * If setting to `online`, validates the user's timetable first — if outside working hours, * the status is set to `offline` instead. */ updateStatus(body: UpdateMeStatusPayload, headers?: Record): Promise { return this.client.put('/me/status', body, headers) } /** * Extends the grace period for the current account when it has expired or is about to expire. * Returns the updated account. */ extendAccountGracePeriod(headers?: Record): Promise { return this.client.post('/me/account/extend-grace-period', undefined, headers) } /** * Registers a OneSignal push notification token for the current user. * Used to enable push notifications on mobile/web clients. */ registerToken(body: RegisterTokenPayload, headers?: Record): Promise { return this.client.post( '/me/one-signal/register-token', body, headers, ) } /** Marks the current user's first login as complete by setting `isFirstLogin` to `false`. */ reportFirstLogin(headers?: Record): Promise { return this.client.post('/me/report-login', undefined, headers) } /** * Sets the current user's status to `online` for the given client and emits a takeover event, * which signals other connected sessions to yield control to this one. */ takeover(body?: TakeoverPayload, headers?: Record): Promise { return this.client.post('/me/takeover', body, headers) } /** * Stores a 16-character OTP secret key on the current user. * This must be called before activating OTP authentication via `activateOtpAuth`. */ setOtpSecretKey(body: SetOtpSecretKeyPayload, headers?: Record): Promise { return this.client.patch('/me/set-otp-secret-key', body, headers) } /** * Activates OTP (two-factor) authentication for the current user. * Requires the `x-digisac-otp` header with a valid TOTP token generated from the secret key * previously set via `setOtpSecretKey`. */ activateOtpAuth(headers?: Record): Promise { return this.client.post('/me/activate-otp-auth', undefined, headers) } }