@go-avro/avro-js 0.0.2-beta.4 → 0.0.2-beta.41

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.
@@ -0,0 +1,92 @@
1
+ import { AvroQueryClient } from '../../client/QueryClient';
2
+ import { useQueryClient, useInfiniteQuery, useMutation } from '@tanstack/react-query';
3
+ AvroQueryClient.prototype.useGetMonths = function (companyGuid, body) {
4
+ const queryClient = useQueryClient();
5
+ const result = useInfiniteQuery({
6
+ queryKey: [
7
+ 'months',
8
+ companyGuid,
9
+ body.amt ?? 50,
10
+ body.known_ids ?? [],
11
+ body.unknown_ids ?? [],
12
+ body.query ?? '',
13
+ body.unbilled ?? true,
14
+ body.billed ?? true,
15
+ body.paid ?? true,
16
+ body.jobId ?? '',
17
+ ],
18
+ initialPageParam: 0,
19
+ getNextPageParam: (lastPage, allPages) => {
20
+ if (lastPage.length < (body.amt ?? 50))
21
+ return undefined;
22
+ return allPages.flat().length; // next offset
23
+ },
24
+ queryFn: ({ pageParam = 0 }) => this.fetchMonths(companyGuid, { ...body, offset: pageParam }),
25
+ });
26
+ if (result.data) {
27
+ result.data.pages.forEach((data_page) => {
28
+ data_page.forEach((month) => {
29
+ queryClient.setQueryData(['month', month.id], month);
30
+ });
31
+ });
32
+ }
33
+ return result;
34
+ };
35
+ AvroQueryClient.prototype.useUpdateMonths = function () {
36
+ const queryClient = useQueryClient();
37
+ return useMutation({
38
+ mutationFn: async ({ companyId, months, action, }) => {
39
+ const monthIds = months.map(month => month.id);
40
+ return this.put(`/company/${companyId}/months`, JSON.stringify({
41
+ months: monthIds,
42
+ billed: true,
43
+ paid: action === "paid",
44
+ }), undefined, {
45
+ "Content-Type": "application/json",
46
+ });
47
+ },
48
+ onMutate: async ({ months, action }) => {
49
+ await queryClient.cancelQueries({ queryKey: ['months'] });
50
+ await queryClient.cancelQueries({ queryKey: ['month'] });
51
+ const previousMonths = queryClient.getQueryData(['months']);
52
+ const previousMonthObjs = months.map(month => queryClient.getQueryData(['month', month.id]));
53
+ const monthIds = months.map(month => month.id);
54
+ monthIds.forEach((monthId, idx) => {
55
+ queryClient.setQueryData(['month', monthId], (oldData) => {
56
+ return oldData
57
+ ? { ...oldData, billed: true, paid: action === "paid" }
58
+ : oldData;
59
+ });
60
+ });
61
+ queryClient.setQueriesData({ queryKey: ['months'] }, (oldData) => {
62
+ if (!oldData)
63
+ return oldData;
64
+ if (oldData.pages) {
65
+ const updatedPages = oldData.pages.map((page) => page.map((month) => monthIds.includes(month.id)
66
+ ? { ...month, billed: true, paid: action === "paid" }
67
+ : month));
68
+ return { ...oldData, pages: updatedPages };
69
+ }
70
+ if (Array.isArray(oldData)) {
71
+ return oldData.map((month) => monthIds.includes(month.id)
72
+ ? { ...month, billed: true, paid: action === "paid" }
73
+ : month);
74
+ }
75
+ return oldData;
76
+ });
77
+ return { previousMonths, previousMonthObjs };
78
+ },
79
+ onError: (err, variables, context) => {
80
+ if (context) {
81
+ queryClient.setQueryData(['months'], context.previousMonths);
82
+ context.previousMonthObjs.forEach((monthObj) => {
83
+ queryClient.setQueryData(['month', monthObj.id], monthObj);
84
+ });
85
+ }
86
+ },
87
+ onSettled: () => {
88
+ queryClient.invalidateQueries({ queryKey: ['months'] });
89
+ queryClient.invalidateQueries({ queryKey: ['month'] });
90
+ },
91
+ });
92
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { AvroQueryClient } from '../../client/QueryClient';
2
+ import { useQuery } from '@tanstack/react-query';
3
+ AvroQueryClient.prototype.useGetRoot = function () {
4
+ return useQuery({
5
+ queryKey: ['health'],
6
+ queryFn: () => this.get('/', undefined) // your async fetch function
7
+ });
8
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,75 @@
1
+ import { AvroQueryClient } from '../../client/QueryClient';
2
+ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
3
+ AvroQueryClient.prototype.useGetRoutes = function (companyGuid, body, total = 0, onProgress) {
4
+ const queryClient = useQueryClient();
5
+ return useQuery({
6
+ queryKey: ['routes', companyGuid, body.amt ?? 50, body.query ?? ""],
7
+ queryFn: async () => {
8
+ if (total === 0) {
9
+ onProgress?.(1);
10
+ return [];
11
+ }
12
+ onProgress?.(0);
13
+ const pageCount = body.amt ? Math.ceil(total / body.amt) : 0;
14
+ let completed = 0;
15
+ const promises = Array.from({ length: pageCount }, (_, i) => this.fetchRoutes(companyGuid, {
16
+ ...body,
17
+ offset: i * (body.amt ?? 0),
18
+ }));
19
+ const trackedPromises = promises.map((promise) => promise.then((result) => {
20
+ completed++;
21
+ const fraction = completed / pageCount;
22
+ onProgress?.(fraction);
23
+ return result;
24
+ }));
25
+ const pages = await Promise.all(trackedPromises);
26
+ const routes = pages.flat();
27
+ return routes;
28
+ },
29
+ enabled: Boolean(companyGuid) && companyGuid.length > 0 && Boolean(total) && total >= 0,
30
+ });
31
+ };
32
+ AvroQueryClient.prototype.useDeleteRoute = function () {
33
+ const queryClient = useQueryClient();
34
+ return useMutation({
35
+ mutationFn: async ({ routeId, }) => {
36
+ return this.delete(`/route/${routeId}`, undefined, {
37
+ "Content-Type": "application/json",
38
+ });
39
+ },
40
+ onMutate: async ({ routeId }) => {
41
+ await queryClient.cancelQueries({ queryKey: ['routes'] });
42
+ await queryClient.cancelQueries({ queryKey: ['route', routeId] });
43
+ const previousRoutes = queryClient.getQueryData(['routes']);
44
+ const previousRoute = queryClient.getQueryData(['route', routeId]);
45
+ queryClient.setQueryData(['route', routeId], undefined);
46
+ queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
47
+ if (!oldData)
48
+ return oldData;
49
+ if (oldData.pages) {
50
+ const updatedPages = oldData.pages.map((page) => page.filter((route) => route.id !== routeId));
51
+ return { ...oldData, pages: updatedPages };
52
+ }
53
+ if (Array.isArray(oldData)) {
54
+ return oldData.filter((route) => route.id !== routeId);
55
+ }
56
+ return oldData;
57
+ });
58
+ return { previousRoutes, previousRoute };
59
+ },
60
+ onError: (_err, variables, context) => {
61
+ const { routeId } = variables;
62
+ if (context?.previousRoutes) {
63
+ queryClient.setQueryData(['routes'], context.previousRoutes);
64
+ }
65
+ if (context?.previousRoute) {
66
+ queryClient.setQueryData(['route', routeId], context.previousRoute);
67
+ }
68
+ },
69
+ onSettled: (_data, _error, variables) => {
70
+ const { routeId } = variables;
71
+ queryClient.invalidateQueries({ queryKey: ['routes'] });
72
+ queryClient.invalidateQueries({ queryKey: ['route', routeId] });
73
+ },
74
+ });
75
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { AvroQueryClient } from "../../client/QueryClient";
2
+ import { useQuery } from "@tanstack/react-query";
3
+ AvroQueryClient.prototype.useGetUserSessions = function () {
4
+ return useQuery({
5
+ queryKey: ['sessions'],
6
+ queryFn: async () => this.get('/session'),
7
+ });
8
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ import { AvroQueryClient } from "../../client/QueryClient";
2
+ import { useQuery } from "@tanstack/react-query";
3
+ AvroQueryClient.prototype.useGetUser = function (userId) {
4
+ return useQuery({
5
+ queryKey: ['user', userId],
6
+ queryFn: () => this.get(`/user/${userId}`),
7
+ enabled: Boolean(userId),
8
+ });
9
+ };
10
+ AvroQueryClient.prototype.useGetSelf = function () {
11
+ return useQuery({
12
+ queryKey: ['user'],
13
+ queryFn: () => this.get(`/user`),
14
+ enabled: Boolean(this),
15
+ });
16
+ };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,18 @@
1
1
  export { AvroQueryClientConfig, AvroQueryClient } from './client/QueryClient';
2
2
  export { AuthManager } from './auth/AuthManager';
3
3
  export { MemoryStorage, LocalStorage } from './auth/storage';
4
+ import './client/core/xhr';
5
+ import './client/core/fetch';
6
+ import './client/core/utils';
7
+ import './client/hooks/root';
8
+ import './client/hooks/jobs';
9
+ import './client/hooks/routes';
10
+ import './client/hooks/events';
11
+ import './client/hooks/months';
12
+ import './client/hooks/bills';
13
+ import './client/hooks/companies';
14
+ import './client/hooks/users';
4
15
  export * from './types/api';
16
+ export * from './types/auth';
5
17
  export * from './types/error';
6
18
  export * from './types/client';
package/dist/index.js CHANGED
@@ -1,6 +1,18 @@
1
1
  export { AvroQueryClient } from './client/QueryClient';
2
2
  export { AuthManager } from './auth/AuthManager';
3
3
  export { MemoryStorage, LocalStorage } from './auth/storage';
4
+ import './client/core/xhr';
5
+ import './client/core/fetch';
6
+ import './client/core/utils';
7
+ import './client/hooks/root';
8
+ import './client/hooks/jobs';
9
+ import './client/hooks/routes';
10
+ import './client/hooks/events';
11
+ import './client/hooks/months';
12
+ import './client/hooks/bills';
13
+ import './client/hooks/companies';
14
+ import './client/hooks/users';
4
15
  export * from './types/api';
16
+ export * from './types/auth';
5
17
  export * from './types/error';
6
18
  export * from './types/client';
@@ -68,12 +68,16 @@ export interface MemberState {
68
68
  }
69
69
  export interface LineItem {
70
70
  id: string;
71
+ line_item_type: "CUSTOM" | "ADDITIONAL_CHARGE" | "EVENT" | "SERVICE_MONTH";
71
72
  name: string;
72
73
  description: string;
73
74
  cost: number | null;
74
75
  amount: number | null;
75
76
  time_created: number;
76
77
  }
78
+ export interface CustomLineItem extends LineItem {
79
+ line_item_type: "CUSTOM";
80
+ }
77
81
  export interface Reaction {
78
82
  id: string;
79
83
  message_id: string;
@@ -193,8 +197,8 @@ export interface User {
193
197
  companies: UserCompanyAssociation[] | null;
194
198
  email: string | null;
195
199
  phone_number: string | null;
196
- time_created: string | null;
197
- time_updated: string | null;
200
+ time_created: number;
201
+ time_updated: number | null;
198
202
  can_send_emails: boolean | null;
199
203
  payment_methods: PaymentMethod[];
200
204
  autopay_payment_types: string[];
@@ -209,16 +213,16 @@ export interface Break {
209
213
  company_billable: boolean;
210
214
  client_billable: boolean;
211
215
  }
212
- export interface ServiceMonth {
213
- id: string;
216
+ export interface ServiceMonth extends LineItem {
217
+ line_item_type: "SERVICE_MONTH";
214
218
  job_name: string;
215
219
  job_id: string | null;
220
+ job_address: string;
221
+ job_labels: string[];
216
222
  bill_id: string | null;
217
- cost: number;
218
223
  billed: boolean;
219
224
  paid: boolean;
220
- amount: number;
221
- time_created: number;
225
+ tasks: string[];
222
226
  time_updated: number | null;
223
227
  }
224
228
  export interface Session {
@@ -294,6 +298,7 @@ export interface Bill {
294
298
  customer_email: string | null;
295
299
  manual_emails: string[][];
296
300
  users: BillUser[];
301
+ paid: boolean;
297
302
  paid_at: number;
298
303
  time_created: number;
299
304
  time_updated: number;
@@ -301,7 +306,7 @@ export interface Bill {
301
306
  intent_created_at: number;
302
307
  intent_last_created_at: number;
303
308
  payment: BillPayment | null;
304
- line_items: LineItem[];
309
+ line_items: CustomLineItem[];
305
310
  months: string[];
306
311
  due_date: number;
307
312
  }
@@ -333,8 +338,8 @@ export interface Company {
333
338
  teams: Team[];
334
339
  emails: Email[];
335
340
  skills: Skill[];
336
- time_created: string;
337
- time_updated: string | null;
341
+ time_created: number;
342
+ time_updated: number | null;
338
343
  users: UserCompanyAssociation[];
339
344
  use_client_side_customer_start_billing: boolean;
340
345
  use_client_side_customer_stop_billing: boolean;
@@ -416,6 +421,7 @@ export interface Job {
416
421
  routes: RouteJob[];
417
422
  subscribers: Subscription[];
418
423
  manual_emails: string[][];
424
+ overdue_time: number;
419
425
  last_completed_event: _Event | null;
420
426
  last_event: _Event | null;
421
427
  labels: string[];
@@ -438,6 +444,7 @@ export interface Task {
438
444
  created_by: UserCompanyAssociation | null;
439
445
  overdueness: number | null;
440
446
  overdue_time: number;
447
+ last_completed_event: _Event | null;
441
448
  last_event: _Event | null;
442
449
  delay: number;
443
450
  skills: string[];
@@ -471,22 +478,17 @@ export interface taskEndInfo {
471
478
  internal_notes: string;
472
479
  external_notes: string;
473
480
  }
474
- export interface AdditionalCharge {
475
- id: string;
476
- time_created: number;
481
+ export interface AdditionalCharge extends LineItem {
482
+ line_item_type: "ADDITIONAL_CHARGE";
477
483
  time_updated: number | null;
478
- name: string;
479
- amount: number;
480
484
  }
481
- export interface _Event {
485
+ export interface _Event extends LineItem {
482
486
  breaks: string[];
483
- id: string;
484
- name: string;
487
+ line_item_type: "EVENT";
485
488
  internal_notes: string;
486
489
  external_notes: string;
487
490
  proofs: string[];
488
491
  tasks: string[];
489
- time_created: number;
490
492
  time_ended: number;
491
493
  time_started: number;
492
494
  time_updated: number | null;
@@ -498,9 +500,75 @@ export interface _Event {
498
500
  additional_charges: AdditionalCharge[];
499
501
  user_id: string;
500
502
  team_id: string;
501
- cost: number;
502
503
  billed: boolean;
503
504
  paid: boolean;
504
505
  autostart: boolean;
505
506
  job_labels: string[];
506
507
  }
508
+ export interface FinancialInsightData {
509
+ start: number;
510
+ end: number;
511
+ unbilled: {
512
+ events: {
513
+ count: number;
514
+ amount: number;
515
+ };
516
+ service_months: {
517
+ count: number;
518
+ amount: number;
519
+ };
520
+ total: number;
521
+ };
522
+ billed_unbilled: {
523
+ events: {
524
+ count: number;
525
+ amount: number;
526
+ };
527
+ service_months: {
528
+ count: number;
529
+ amount: number;
530
+ };
531
+ total: number;
532
+ };
533
+ paid_unbilled: {
534
+ events: {
535
+ count: number;
536
+ amount: number;
537
+ };
538
+ service_months: {
539
+ count: number;
540
+ amount: number;
541
+ };
542
+ total: number;
543
+ };
544
+ prepaid: {
545
+ events: {
546
+ count: number;
547
+ amount: number;
548
+ };
549
+ service_months: {
550
+ count: number;
551
+ amount: number;
552
+ };
553
+ total: number;
554
+ };
555
+ accounts_receivable: {
556
+ overdue: number;
557
+ not_overdue: number;
558
+ };
559
+ cash: number;
560
+ }
561
+ export interface EventInsightData {
562
+ start: number;
563
+ end: number;
564
+ events_per_job: {
565
+ id: string;
566
+ name: string;
567
+ event_count: number;
568
+ }[];
569
+ events_per_team: {
570
+ team_id: string;
571
+ event_count: number;
572
+ }[];
573
+ total_events: number;
574
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-avro/avro-js",
3
- "version": "0.0.2-beta.4",
3
+ "version": "0.0.2-beta.41",
4
4
  "description": "JS client for Avro backend integration.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -33,6 +33,7 @@
33
33
  "license": "CC-BY-SA-4.0",
34
34
  "devDependencies": {
35
35
  "@types/jest": "^29.0.0",
36
+ "@types/react": "^19.2.2",
36
37
  "@typescript-eslint/eslint-plugin": "^8.38.0",
37
38
  "@typescript-eslint/parser": "^8.38.0",
38
39
  "eslint": "^8.57.1",
@@ -50,5 +51,8 @@
50
51
  ],
51
52
  "publishConfig": {
52
53
  "access": "public"
54
+ },
55
+ "dependencies": {
56
+ "@tanstack/react-query": "^5.90.2"
53
57
  }
54
58
  }