@go-avro/avro-js 0.0.4-beta.34 → 0.0.4-beta.36

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.
@@ -1,6 +1,6 @@
1
1
  import { useInfiniteQuery, useQuery, useMutation } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../../client/QueryClient';
3
- import { LineItemStatus } from '../../types/api';
3
+ import { _Event, LineItemStatus } from '../../types/api';
4
4
  AvroQueryClient.prototype.useGetEvents = function (body) {
5
5
  const queryClient = this.getQueryClient();
6
6
  const result = useInfiniteQuery({
@@ -23,7 +23,7 @@ AvroQueryClient.prototype.useGetEvents = function (body) {
23
23
  return undefined;
24
24
  return allPages.flat().length; // next offset
25
25
  },
26
- queryFn: ({ pageParam = 0 }) => this.fetchEvents({ ...body, job_id: body.jobId, offset: pageParam }),
26
+ queryFn: ({ pageParam = 0 }) => this.fetchEvents({ ...body, job_id: body.jobId, offset: pageParam }).then((events) => events.map((event) => new _Event(event))),
27
27
  enabled: body.enabled ?? true,
28
28
  });
29
29
  if (result.data) {
@@ -38,7 +38,7 @@ AvroQueryClient.prototype.useGetEvents = function (body) {
38
38
  AvroQueryClient.prototype.useGetEvent = function (eventId) {
39
39
  return useQuery({
40
40
  queryKey: ['event', eventId],
41
- queryFn: () => this.get(`/event/${eventId}`),
41
+ queryFn: () => this.get(`/event/${eventId}`).then((event) => new _Event(event)),
42
42
  enabled: Boolean(eventId),
43
43
  });
44
44
  };
@@ -55,18 +55,23 @@ AvroQueryClient.prototype.useCreateEvent = function () {
55
55
  const previousEvents = queryClient.getQueryData(['events']);
56
56
  const previousJob = queryClient.getQueryData(['job', eventData.job_id]);
57
57
  const previousJobs = queryClient.getQueryData(['jobs']);
58
+ const optimisticEvent = new _Event({
59
+ ...eventData,
60
+ id: Math.random().toString(36).substring(2, 11),
61
+ company_id: this.companyId,
62
+ });
58
63
  if (previousJob) {
59
64
  const updatedJob = {
60
65
  ...previousJob,
61
- current_event: eventData,
62
- last_completed_event: (eventData.time_ended ?? -1) > -1 ? eventData : previousJob.last_completed_event,
66
+ current_event: optimisticEvent,
67
+ last_completed_event: (optimisticEvent.time_ended ?? -1) > -1 ? optimisticEvent : previousJob.last_completed_event,
63
68
  };
64
69
  updatedJob.tasks = previousJob.tasks.map((task) => {
65
70
  if (eventData.tasks?.includes(task.id ?? "")) {
66
71
  return {
67
72
  ...task,
68
- current_event: eventData,
69
- last_completed_event: (eventData.time_ended ?? -1) > -1 ? eventData : task.last_completed_event,
73
+ current_event: optimisticEvent,
74
+ last_completed_event: (optimisticEvent.time_ended ?? -1) > -1 ? optimisticEvent : task.last_completed_event,
70
75
  };
71
76
  }
72
77
  return task;
@@ -84,11 +89,7 @@ AvroQueryClient.prototype.useCreateEvent = function () {
84
89
  ...oldData,
85
90
  pages: [
86
91
  [
87
- {
88
- ...eventData,
89
- id: Math.random().toString(36).substring(2, 11),
90
- company_id: this.companyId,
91
- },
92
+ optimisticEvent,
92
93
  ...firstPage,
93
94
  ],
94
95
  ...oldData.pages.slice(1),
@@ -97,11 +98,7 @@ AvroQueryClient.prototype.useCreateEvent = function () {
97
98
  }
98
99
  if (Array.isArray(oldData)) {
99
100
  return [
100
- {
101
- ...eventData,
102
- id: Math.random().toString(36).substring(2, 11),
103
- company_id: this.companyId,
104
- },
101
+ optimisticEvent,
105
102
  ...oldData,
106
103
  ];
107
104
  }
@@ -3,5 +3,6 @@ export interface ServiceMonth extends LineItem {
3
3
  line_item_type: "SERVICE_MONTH";
4
4
  bill_id: string | null;
5
5
  tasks: string[];
6
+ task_names: string[];
6
7
  time_updated: number | null;
7
8
  }
@@ -1,19 +1,26 @@
1
1
  import { LineItem } from "../../types/api/LineItem";
2
2
  import { AdditionalCharge } from "../../types/api/AdditionalCharge";
3
3
  import { UserEvent } from "../../types/api/UserEvent";
4
- export interface _Event extends LineItem {
5
- breaks: string[];
6
- line_item_type: "EVENT";
7
- internal_notes: string;
8
- external_notes: string;
9
- proofs: string[];
10
- tasks: string[];
11
- time_ended: number | null;
12
- time_started: number;
13
- time_updated: number | null;
14
- bill_id: string;
15
- billed_amount: number;
16
- additional_charges: AdditionalCharge[];
17
- users: UserEvent[];
18
- autostart: boolean;
4
+ declare module '../../types/api/_Event' {
5
+ interface _Event extends LineItem {
6
+ breaks: string[];
7
+ line_item_type: "EVENT";
8
+ internal_notes: string;
9
+ external_notes: string;
10
+ proofs: string[];
11
+ tasks: string[];
12
+ task_names: string[];
13
+ time_ended: number | null;
14
+ time_started: number;
15
+ time_updated: number | null;
16
+ bill_id: string;
17
+ billed_amount: number;
18
+ additional_charges: AdditionalCharge[];
19
+ users: UserEvent[];
20
+ autostart: boolean;
21
+ }
22
+ }
23
+ export declare class _Event extends LineItem {
24
+ constructor(init?: Partial<_Event>);
25
+ getCost: () => number;
19
26
  }
@@ -1 +1,18 @@
1
- export {};
1
+ import { LineItem } from "../../types/api/LineItem";
2
+ export class _Event extends LineItem {
3
+ constructor(init) {
4
+ super(init);
5
+ this.getCost = () => {
6
+ const additionalChargesCost = this.additional_charges.reduce((acc, charge) => acc + (charge.cost ?? 0) * (charge.amount ?? 0), 0);
7
+ return (this.cost * this.amount) + additionalChargesCost;
8
+ };
9
+ Object.assign(this, init);
10
+ this.breaks = init?.breaks ?? [];
11
+ this.proofs = init?.proofs ?? [];
12
+ this.tasks = init?.tasks ?? [];
13
+ this.task_names = init?.task_names ?? [];
14
+ this.additional_charges = init?.additional_charges ?? [];
15
+ this.users = init?.users ?? [];
16
+ this.autostart = init?.autostart ?? false;
17
+ }
18
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-avro/avro-js",
3
- "version": "0.0.4-beta.34",
3
+ "version": "0.0.4-beta.36",
4
4
  "description": "JS client for Avro backend integration.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",