@go-avro/avro-js 0.0.2-beta.164 → 0.0.2-beta.166

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,6 +19,6 @@ export declare class AuthManager {
19
19
  setCache(data: Partial<CacheData>): Promise<void>;
20
20
  getCache(key?: keyof CacheData): Promise<CacheData | string | null>;
21
21
  clearCache(): Promise<void>;
22
- getCompanyId(): Promise<string | null>;
22
+ getCompanyId(): Promise<string | undefined>;
23
23
  setCompanyId(companyId: string): Promise<void[]>;
24
24
  }
@@ -129,14 +129,14 @@ export class AuthManager {
129
129
  }
130
130
  async getCompanyId() {
131
131
  if (!this.storages.length) {
132
- return null;
132
+ return undefined;
133
133
  }
134
134
  for (const storage of this.storages) {
135
135
  const companyId = await storage.get('companyId');
136
136
  if (companyId && typeof companyId === 'string')
137
137
  return companyId;
138
138
  }
139
- return null;
139
+ return undefined;
140
140
  }
141
141
  async setCompanyId(companyId) {
142
142
  return Promise.all(this.storages.map(s => s.set({ companyId })));
@@ -1,7 +1,7 @@
1
1
  import { Socket } from 'socket.io-client';
2
2
  import { InfiniteData, QueryClient, UseInfiniteQueryResult, useMutation, UseQueryResult } from '@tanstack/react-query';
3
3
  import { AuthManager } from '../auth/AuthManager';
4
- import { _Event, ApiInfo, Avro, Bill, Break, Chat, Company, FinancialInsightData, Job, EventInsightData, LoginResponse, Message, Plan, Route, ServiceMonth, Session, Team, User, UserCompanyAssociation, Skill, Group, Label, RouteScheduleConfig } from '../types/api';
4
+ import { _Event, ApiInfo, Avro, Bill, Break, Chat, Company, FinancialInsightData, Job, EventInsightData, LoginResponse, Message, Plan, Route, ServiceMonth, Session, Team, User, UserCompanyAssociation, Skill, Group, Label, RouteScheduleConfig, CatalogItem } from '../types/api';
5
5
  import { AuthState, Tokens } from '../types/auth';
6
6
  import { CancelToken, RetryStrategy } from '../types/client';
7
7
  import { StandardError } from '../types/error';
@@ -20,7 +20,11 @@ declare module '../client/QueryClient' {
20
20
  getDelay(strategy: RetryStrategy, attempt: number): number;
21
21
  sleep(ms: number): Promise<void>;
22
22
  useGetRoot(): UseQueryResult<ApiInfo, StandardError>;
23
- useGetJobs(onProgress?: (fraction: number) => void): UseQueryResult<Job[], StandardError>;
23
+ useGetJobs(params: {
24
+ companyId?: string;
25
+ numJobs?: number;
26
+ onProgress?: (fraction: number) => void;
27
+ }): UseQueryResult<Job[], StandardError>;
24
28
  getJobsFromCache(queryClient?: QueryClient): Job[];
25
29
  useGetInfiniteJobs(body: {
26
30
  amt?: number;
@@ -120,6 +124,7 @@ declare module '../client/QueryClient' {
120
124
  useGetBill(billId: string): UseQueryResult<Bill, StandardError>;
121
125
  useGetRoute(routeId: string): UseQueryResult<Route, StandardError>;
122
126
  useGetChat(chatId: string): UseQueryResult<Chat, StandardError>;
127
+ useGetCatalogItem(catalogItemId: string): UseQueryResult<CatalogItem, StandardError>;
123
128
  useGetUserSessions(): UseQueryResult<Session[], StandardError>;
124
129
  useGetAvro(): UseQueryResult<Avro, StandardError>;
125
130
  useSearchUsers(searchUsername: string): UseQueryResult<User[], StandardError>;
@@ -176,6 +181,11 @@ declare module '../client/QueryClient' {
176
181
  logo: File | null;
177
182
  }>;
178
183
  }>>;
184
+ useCreateCatalogItem(): ReturnType<typeof useMutation<{
185
+ id: string;
186
+ }, StandardError, {
187
+ data: Partial<CatalogItem>;
188
+ }>>;
179
189
  useCreateJob(): ReturnType<typeof useMutation<{
180
190
  id: string;
181
191
  }, StandardError, {
@@ -206,7 +216,6 @@ declare module '../client/QueryClient' {
206
216
  breakData: Partial<Break>;
207
217
  }>>;
208
218
  useManageJobs(): ReturnType<typeof useMutation<void, StandardError, {
209
- companyId: string;
210
219
  jobs: Partial<Job>[];
211
220
  }>>;
212
221
  useScheduleRoutes(): ReturnType<typeof useMutation<{
@@ -243,6 +252,12 @@ declare module '../client/QueryClient' {
243
252
  labelId: string;
244
253
  labelData: Partial<Label>;
245
254
  }>>;
255
+ useUpdateCatalogItem(): ReturnType<typeof useMutation<{
256
+ msg: string;
257
+ }, StandardError, {
258
+ catalogItemId: string;
259
+ data: Partial<CatalogItem>;
260
+ }>>;
246
261
  useUpdateJob(): ReturnType<typeof useMutation<{
247
262
  msg: string;
248
263
  }, StandardError, {
@@ -291,6 +306,11 @@ declare module '../client/QueryClient' {
291
306
  user_id: string;
292
307
  data: Partial<UserCompanyAssociation>;
293
308
  }>>;
309
+ useDeleteCatalogItem(): ReturnType<typeof useMutation<{
310
+ msg: string;
311
+ }, StandardError, {
312
+ catalogItemId: string;
313
+ }>>;
294
314
  useUpdateSessionBreak(): ReturnType<typeof useMutation<{
295
315
  msg: string;
296
316
  }, StandardError, {
@@ -300,6 +320,11 @@ declare module '../client/QueryClient' {
300
320
  useUpdateSelf(): ReturnType<typeof useMutation<{
301
321
  msg: string;
302
322
  }, StandardError, Partial<User>>>;
323
+ useDeleteJobs(): ReturnType<typeof useMutation<{
324
+ msg: string;
325
+ }, StandardError, {
326
+ ids: string[];
327
+ }>>;
303
328
  useDeleteJob(): ReturnType<typeof useMutation<{
304
329
  msg: string;
305
330
  }, StandardError, {
@@ -373,8 +398,8 @@ export declare class AvroQueryClient {
373
398
  protected config: Required<AvroQueryClientConfig>;
374
399
  readonly socket: Socket;
375
400
  _authState: AuthState;
376
- companyId: string | null;
377
- company: Company | null;
401
+ companyId: string | undefined;
402
+ company: Company | undefined;
378
403
  private authStateListeners;
379
404
  constructor(config: AvroQueryClientConfig);
380
405
  emit(eventName: string, data: unknown): void;
@@ -412,7 +437,7 @@ export declare class AvroQueryClient {
412
437
  setCache(data: Partial<CacheData>): Promise<void>;
413
438
  getCache(key?: keyof CacheData | undefined): Promise<CacheData | string | null>;
414
439
  setCompanyId(companyId: string): Promise<void[]>;
415
- getCompanyId(): Promise<string | null>;
440
+ getCompanyId(): Promise<string | undefined>;
416
441
  clearCache(): Promise<void>;
417
442
  onAuthStateChange(cb: (v: AuthState) => void): () => void;
418
443
  offAuthStateChange(cb: (v: AuthState) => void): void;
@@ -428,7 +453,7 @@ export declare class AvroQueryClient {
428
453
  query?: string;
429
454
  offset?: number;
430
455
  route_id?: string;
431
- }, cancelToken?: CancelToken, headers?: Record<string, string>): Promise<any>;
456
+ }, companyId?: string, cancelToken?: CancelToken, headers?: Record<string, string>): Promise<any>;
432
457
  fetchChats(body?: {
433
458
  amt?: number;
434
459
  known_ids?: string[];
@@ -6,8 +6,8 @@ import { StandardError } from '../types/error';
6
6
  export class AvroQueryClient {
7
7
  constructor(config) {
8
8
  this._authState = AuthState.UNKNOWN;
9
- this.companyId = null;
10
- this.company = null;
9
+ this.companyId = undefined;
10
+ this.company = undefined;
11
11
  this.authStateListeners = [];
12
12
  this.config = {
13
13
  baseUrl: config.baseUrl,
@@ -238,11 +238,12 @@ export class AvroQueryClient {
238
238
  }
239
239
  });
240
240
  }
241
- fetchJobs(body = {}, cancelToken, headers = {}) {
242
- if (!this.companyId || this.companyId.trim() === '') {
241
+ fetchJobs(body = {}, companyId, cancelToken, headers = {}) {
242
+ const companyIdToUse = companyId ?? this.companyId;
243
+ if (!companyIdToUse || companyIdToUse.trim() === '') {
243
244
  throw new StandardError(400, 'Company ID is required');
244
245
  }
245
- return this._fetch('POST', `/company/${this.companyId}/jobs`, JSON.stringify(body), cancelToken, {
246
+ return this._fetch('POST', `/company/${companyIdToUse}/jobs`, JSON.stringify(body), cancelToken, {
246
247
  ...headers,
247
248
  'Content-Type': 'application/json',
248
249
  })
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,90 @@
1
+ import { useMutation } from '@tanstack/react-query';
2
+ import { AvroQueryClient } from '../../client/QueryClient';
3
+ AvroQueryClient.prototype.useCreateCatalogItem = function () {
4
+ const queryClient = this.getQueryClient();
5
+ return useMutation({
6
+ mutationFn: async ({ data }) => {
7
+ return this.post(`/company/${this.companyId}/catalog`, JSON.stringify(data), undefined, { 'Content-Type': 'application/json' });
8
+ },
9
+ onMutate: async () => {
10
+ await queryClient.cancelQueries({ queryKey: ['catalog_items', this.companyId] });
11
+ const previousItems = queryClient.getQueryData(['catalog_items', this.companyId]);
12
+ return { previousItems };
13
+ },
14
+ onError: (_err, _variables, context) => {
15
+ if (context?.previousItems) {
16
+ queryClient.setQueryData(['catalog_items', this.companyId], context.previousItems);
17
+ }
18
+ },
19
+ onSettled: () => {
20
+ queryClient.invalidateQueries({ queryKey: ['catalog_items', this.companyId] });
21
+ },
22
+ });
23
+ };
24
+ AvroQueryClient.prototype.useUpdateCatalogItem = function () {
25
+ const queryClient = this.getQueryClient();
26
+ return useMutation({
27
+ mutationFn: ({ catalogItemId, data }) => {
28
+ return this.put(`/catalog_item/${catalogItemId}`, JSON.stringify(data), undefined, {
29
+ "Content-Type": "application/json",
30
+ });
31
+ },
32
+ onMutate: async ({ catalogItemId, data }) => {
33
+ await queryClient.cancelQueries({ queryKey: ['catalog_items'] });
34
+ await queryClient.cancelQueries({ queryKey: ['catalog_item', catalogItemId] });
35
+ const previousItems = queryClient.getQueryData(['catalog_items']);
36
+ const previousItem = queryClient.getQueryData(['catalog_item', catalogItemId]);
37
+ queryClient.setQueryData(['catalog_item', catalogItemId], (oldData) => oldData ? { ...oldData, ...data } : undefined);
38
+ queryClient.setQueriesData({ queryKey: ['catalog_items'] }, (oldData) => {
39
+ if (!oldData)
40
+ return oldData;
41
+ if (oldData.pages) {
42
+ return {
43
+ ...oldData,
44
+ pages: oldData.pages.map((page) => page.map((item) => item.id === catalogItemId ? { ...item, ...data } : item)),
45
+ };
46
+ }
47
+ if (Array.isArray(oldData)) {
48
+ return oldData.map((item) => item.id === catalogItemId ? { ...item, ...data } : item);
49
+ }
50
+ return oldData;
51
+ });
52
+ return { previousItems, previousItem };
53
+ },
54
+ onError: (err, variables, context) => {
55
+ const { catalogItemId } = variables;
56
+ if (context?.previousItems) {
57
+ queryClient.setQueryData(['catalog_items'], context.previousItems);
58
+ }
59
+ if (context?.previousItem) {
60
+ queryClient.setQueryData(['catalog_item', catalogItemId], context.previousItem);
61
+ }
62
+ },
63
+ onSettled: (_data, _error, variables) => {
64
+ const { catalogItemId } = variables;
65
+ queryClient.invalidateQueries({ queryKey: ['catalog_items'] });
66
+ queryClient.invalidateQueries({ queryKey: ['catalog_item', catalogItemId] });
67
+ },
68
+ });
69
+ };
70
+ AvroQueryClient.prototype.useDeleteCatalogItem = function () {
71
+ const queryClient = this.getQueryClient();
72
+ return useMutation({
73
+ mutationFn: async ({ catalogItemId }) => {
74
+ return this.delete(`/catalog_item/${catalogItemId}`);
75
+ },
76
+ onMutate: async ({ catalogItemId }) => {
77
+ await queryClient.cancelQueries({ queryKey: ['catalog_items'] });
78
+ const previousItems = queryClient.getQueryData(['catalog_items']);
79
+ return { previousItems };
80
+ },
81
+ onError: (_err, _variables, context) => {
82
+ if (context?.previousItems) {
83
+ queryClient.setQueryData(['catalog_items'], context.previousItems);
84
+ }
85
+ },
86
+ onSettled: (_data, _error) => {
87
+ queryClient.invalidateQueries({ queryKey: ['catalog_items'] });
88
+ },
89
+ });
90
+ };
@@ -6,20 +6,22 @@ AvroQueryClient.prototype.getJobsFromCache = function (queryClient) {
6
6
  }
7
7
  return (queryClient.getQueryData(['jobs', this.companyId, this.company?.num_jobs]) ?? []);
8
8
  };
9
- AvroQueryClient.prototype.useGetJobs = function (onProgress) {
9
+ AvroQueryClient.prototype.useGetJobs = function (params) {
10
10
  const queryClient = this.getQueryClient();
11
11
  const amt = 50;
12
+ const company_id = params.companyId ?? this.companyId;
13
+ const num_jobs = params.numJobs ?? this.company?.num_jobs ?? 0;
12
14
  return useQuery({
13
- queryKey: ['jobs', this.companyId, this.company?.num_jobs],
15
+ queryKey: ['jobs', company_id, num_jobs],
14
16
  queryFn: async () => {
15
- onProgress?.(0);
16
- const pageCount = amt ? Math.ceil((this.company?.num_jobs ?? 0) / amt) + 1 : 1;
17
+ params.onProgress?.(0);
18
+ const pageCount = amt ? Math.ceil((num_jobs ?? 0) / amt) + 1 : 1;
17
19
  let completed = 0;
18
- const promises = Array.from({ length: pageCount }, (_, i) => this.fetchJobs({ offset: i * (amt ?? 1), amt }));
20
+ const promises = Array.from({ length: pageCount }, (_, i) => this.fetchJobs({ offset: i * (amt ?? 1), amt }, company_id));
19
21
  const trackedPromises = promises.map((promise) => promise.then((result) => {
20
22
  completed++;
21
23
  const fraction = completed / pageCount;
22
- onProgress?.(fraction);
24
+ params.onProgress?.(fraction);
23
25
  return result;
24
26
  }));
25
27
  const pages = await Promise.all(trackedPromises);
@@ -122,12 +124,12 @@ AvroQueryClient.prototype.useCreateJob = function () {
122
124
  AvroQueryClient.prototype.useManageJobs = function () {
123
125
  const queryClient = this.getQueryClient();
124
126
  return useMutation({
125
- mutationFn: ({ companyId, jobs }) => {
126
- return this.post(`/company/${companyId}/jobs/manage`, JSON.stringify({ jobs }), undefined, {
127
+ mutationFn: ({ jobs }) => {
128
+ return this.post(`/company/${this.companyId}/jobs/manage`, JSON.stringify({ jobs }), undefined, {
127
129
  "Content-Type": "application/json",
128
130
  });
129
131
  },
130
- onMutate: async ({ companyId, jobs }) => {
132
+ onMutate: async ({ jobs }) => {
131
133
  await queryClient.cancelQueries({ queryKey: ['jobs'] });
132
134
  const previousJobs = queryClient.getQueryData(['jobs']);
133
135
  queryClient.setQueryData(['jobs'], (oldData) => {
@@ -208,6 +210,44 @@ AvroQueryClient.prototype.useUpdateJob = function () {
208
210
  },
209
211
  });
210
212
  };
213
+ AvroQueryClient.prototype.useDeleteJobs = function () {
214
+ const queryClient = this.getQueryClient();
215
+ return useMutation({
216
+ mutationFn: ({ ids }) => {
217
+ return this.post(`/company/${this.companyId}/jobs/delete`, JSON.stringify({ ids }), undefined, {
218
+ "Content-Type": "application/json",
219
+ });
220
+ },
221
+ onMutate: async ({ ids }) => {
222
+ await queryClient.cancelQueries({ queryKey: ['jobs'] });
223
+ const previousJobs = queryClient.getQueryData(['jobs']);
224
+ queryClient.setQueryData(['jobs'], (oldData) => {
225
+ if (!oldData)
226
+ return oldData;
227
+ if (oldData.pages) {
228
+ const updatedPages = oldData.pages.map((page) => page.filter((job) => !ids.includes(job.id)));
229
+ return { ...oldData, pages: updatedPages };
230
+ }
231
+ if (Array.isArray(oldData)) {
232
+ return oldData.filter((job) => !ids.includes(job.id));
233
+ }
234
+ return oldData;
235
+ });
236
+ return { previousJobs };
237
+ },
238
+ onError: (_err, variables, context) => {
239
+ if (context?.previousJobs) {
240
+ queryClient.setQueryData(['jobs'], context.previousJobs);
241
+ }
242
+ },
243
+ onSettled: (_data, _error, _variables) => {
244
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
245
+ queryClient.invalidateQueries({ queryKey: ['routes'] });
246
+ queryClient.invalidateQueries({ queryKey: ['job'] });
247
+ queryClient.invalidateQueries({ queryKey: ['route'] });
248
+ },
249
+ });
250
+ };
211
251
  AvroQueryClient.prototype.useDeleteJob = function () {
212
252
  const queryClient = this.getQueryClient();
213
253
  return useMutation({
package/dist/index.d.ts CHANGED
@@ -15,6 +15,7 @@ import './client/hooks/companies';
15
15
  import './client/hooks/users';
16
16
  import './client/hooks/sessions';
17
17
  import './client/hooks/chats';
18
+ import './client/hooks/catalog_items';
18
19
  import './client/hooks/messages';
19
20
  import './client/hooks/plans';
20
21
  import './client/hooks/analytics';
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@ import './client/hooks/companies';
15
15
  import './client/hooks/users';
16
16
  import './client/hooks/sessions';
17
17
  import './client/hooks/chats';
18
+ import './client/hooks/catalog_items';
18
19
  import './client/hooks/messages';
19
20
  import './client/hooks/plans';
20
21
  import './client/hooks/analytics';
@@ -7,6 +7,24 @@ export interface ApiInfo {
7
7
  version: string;
8
8
  vroom_healthy: boolean;
9
9
  }
10
+ export interface CatalogItem {
11
+ id: string;
12
+ name: string;
13
+ company_id: string;
14
+ description: string;
15
+ industry: string;
16
+ price_format: string;
17
+ payment_options: PaymentOption[];
18
+ }
19
+ export interface PaymentOption {
20
+ name: string;
21
+ id: string;
22
+ is_prepay: boolean;
23
+ price_under_prepay?: number;
24
+ price: number;
25
+ recommended_prepay_count?: number;
26
+ mode: "MONTH" | "SERVICE";
27
+ }
10
28
  export interface PaymentMethod {
11
29
  allow_redisplay: string;
12
30
  autopay: boolean;
@@ -438,6 +456,7 @@ export interface Company {
438
456
  email: string;
439
457
  emails: Email[];
440
458
  skills: Skill[];
459
+ catalog_items: CatalogItem[];
441
460
  time_created: number;
442
461
  time_updated: number | null;
443
462
  users: UserCompanyAssociation[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-avro/avro-js",
3
- "version": "0.0.2-beta.164",
3
+ "version": "0.0.2-beta.166",
4
4
  "description": "JS client for Avro backend integration.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",