📦 EqualifyEverything / equalify-dashboard

📄 properties.ts · 183 lines
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183import { del, get, post, put } from 'aws-amplify/api';

interface ApiResponse<T> {
  status: string;
  result: T;
  total?: number;
}

type Node = {
  url: string;
};

type Nodes = {
  nodes: Node[];
};

interface Property {
  id: string;
  name: string;
  propertyUrl: string;
  lastProcessed: string;
  archived: boolean;
  discovery: null | 'single' | 'sitemap' | 'discovery_process';
  processed: null | string;
  updatedAt: string;
  createdAt: string;
}

const API_NAME = 'auth';

/**
 * Fetch all properties
 * @returns {Promise<Property[]>} List of properties
 * @throws Will throw an error if the fetch fails
 */
export const getProperties = async (): Promise<Property[]> => {
  try {
    const response = await get({
      apiName: API_NAME,
      path: '/get/properties',
    }).response;

    const { body } = response;
    const { result } = (await body.json()) as unknown as ApiResponse<
      Property[]
    >;
    return result;
  } catch (error) {
    console.error('Error fetching properties', error);
    throw error;
  }
};

/**
 * Fetch property by ID
 * @param {string} propertyId - The ID of the property to fetch
 * @returns {Promise<Property>} The fetched property
 * @throws Will throw an error if the fetch fails
 */
export const getPropertyById = async (
  propertyId: string,
): Promise<Property> => {
  try {
    const response = await get({
      apiName: API_NAME,
      path: `/get/properties`,
      options: {
        queryParams: { propertyIds: propertyId },
      },
    }).response;

    const { body } = response;
    const { result } = (await body.json()) as unknown as ApiResponse<
      Property[]
    >;
    return result[0];
  } catch (error) {
    console.error('Error fetching property by ID', error);
    throw error;
  }
};

/**
 * Add a new property
 * @param {string} propertyName - The name of the property
 * @param {string} propertyUrl - The URL of the property
 * @param {'single' | 'sitemap' |'discovery_process'} propertyDiscovery - The discovery method of the property
 * @returns {Promise<{ result: Property; status: string }>} The added property and status
 * @throws Will throw an error if the addition fails
 */
export const addProperty = async (
  propertyName: string,
  propertyUrl: string,
  propertyDiscovery: 'single' | 'sitemap' | 'discovery_process',
): Promise<{ result: Property; status: string }> => {
  try {
    const response = await post({
      apiName: API_NAME,
      path: '/add/properties',
      options: {
        body: {
          propertyName,
          propertyUrl,
          propertyDiscovery,
        },
      },
    }).response;

    const { body, statusCode } = response;
    const { result } = (await body.json()) as unknown as ApiResponse<Property>;
    return { result, status: statusCode === 200 ? 'success' : 'error' };
  } catch (error) {
    console.error('Error adding property', error);
    throw error;
  }
};

/**
 * Update a property
 * @param {string} propertyId - The ID of the property to update
 * @param {string} propertyName - The new name of the property
 * @param {string} propertyUrl - The new URL of the property
 * @param {string} propertyDiscovery - The discovery of the property
 * @returns {Promise<{ result: Property; status: string }>} The updated property and status
 * @throws Will throw an error if the update fails
 */
export const updateProperty = async (
  propertyId: string,
  propertyName: string,
  propertyUrl: string,
  propertyDiscovery: string,
): Promise<{ result: Property; status: string }> => {
  try {
    const response = await put({
      apiName: API_NAME,
      path: '/update/properties',
      options: {
        body: {
          propertyId,
          propertyName,
          propertyUrl,
          propertyDiscovery,
        },
      },
    }).response;

    const { body, statusCode } = response;
    const { result } = (await body.json()) as unknown as ApiResponse<Property>;
    return { result, status: statusCode === 200 ? 'success' : 'error' };
  } catch (error) {
    console.error('Error updating property', error);
    throw error;
  }
};

/**
 * Delete a property
 * @param {string} propertyId - The ID of the property to delete
 * @returns {Promise<{ status: string }>} The status of the deletion
 * @throws Will throw an error if the deletion fails
 */
export const deleteProperty = async (
  propertyId: string,
): Promise<{ status: string }> => {
  try {
    const response = await del({
      apiName: API_NAME,
      path: '/delete/properties',
      options: {
        queryParams: {
          propertyId,
        },
      },
    }).response;

    const { statusCode } = response;
    return { status: statusCode === 200 ? 'success' : 'error' };
  } catch (error) {
    console.error('Error deleting property', error);
    throw error;
  }
};