File size: 2,146 Bytes
f8b5d42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { API_BASE } from "@/utils/constants";
import { baseHeaders } from "@/utils/request";

/**
 * @typedef {Object} MobileConnection
 * @property {string} id - The database ID of the device.
 * @property {string} deviceId - The device ID of the device.
 * @property {string} deviceOs - The operating system of the device.
 * @property {boolean} approved - Whether the device is approved.
 * @property {string} createdAt - The date and time the device was created.
 */

const MobileConnection = {
  /**
   * Get the connection info for the mobile app.
   * @returns {Promise<{connectionUrl: string|null}>} The connection info.
   */
  getConnectionInfo: async function () {
    return await fetch(`${API_BASE}/mobile/connect-info`, {
      headers: baseHeaders(),
    })
      .then((res) => res.json())
      .catch(() => false);
  },

  /**
   * Get all the devices from the database.
   * @returns {Promise<MobileDevice[]>} The devices.
   */
  getDevices: async function () {
    return await fetch(`${API_BASE}/mobile/devices`, {
      headers: baseHeaders(),
    })
      .then((res) => res.json())
      .then((res) => res.devices || [])
      .catch(() => []);
  },

  /**
   * Delete a device from the database.
   * @param {string} deviceId - The database ID of the device to delete.
   * @returns {Promise<{message: string}>} The deleted device.
   */
  deleteDevice: async function (id) {
    return await fetch(`${API_BASE}/mobile/${id}`, {
      method: "DELETE",
      headers: baseHeaders(),
    })
      .then((res) => res.json())
      .catch(() => false);
  },

  /**
   * Update a device in the database.
   * @param {string} id - The database ID of the device to update.
   * @param {Object} updates - The updates to apply to the device.
   * @returns {Promise<{updates: MobileDevice}>} The updated device.
   */
  updateDevice: async function (id, updates = {}) {
    return await fetch(`${API_BASE}/mobile/update/${id}`, {
      method: "POST",
      body: JSON.stringify(updates),
      headers: baseHeaders(),
    })
      .then((res) => res.json())
      .catch(() => false);
  },
};

export default MobileConnection;