@go-avro/avro-js 0.0.4-beta.20 → 0.0.4-beta.22

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 { useMutation, useQuery, useInfiniteQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../../client/QueryClient';
3
- import { Task } from '../../types/api';
3
+ import { Job } from '../../types/api';
4
4
  AvroQueryClient.prototype.getJobsFromCache = function (queryClient) {
5
5
  if (!queryClient) {
6
6
  queryClient = this.getQueryClient();
@@ -26,9 +26,8 @@ AvroQueryClient.prototype.useGetJobs = function (params) {
26
26
  return result;
27
27
  }));
28
28
  const pages = await Promise.all(trackedPromises);
29
- const jobs = pages.flat();
29
+ const jobs = pages.flat().map((job) => new Job(job));
30
30
  jobs.forEach((job) => {
31
- job.tasks = job.tasks.map((task) => new Task(task));
32
31
  job.current_event = job.tasks.reduce((latest, task) => {
33
32
  return task.current_event && (!latest || task.current_event.time_started > latest.time_started) ? task.current_event : latest;
34
33
  }, job.current_event);
@@ -60,13 +59,12 @@ AvroQueryClient.prototype.useGetInfiniteJobs = function (body, onProgress) {
60
59
  ...body,
61
60
  route_id: body.routeId,
62
61
  offset: pageParam,
63
- });
62
+ }).then((jobs) => jobs.map((job) => new Job(job)));
64
63
  },
65
64
  });
66
65
  if (result.data) {
67
66
  result.data.pages.forEach((data_page) => {
68
67
  data_page.forEach((job) => {
69
- job.tasks = job.tasks.map((task) => new Task(task));
70
68
  job.overdue_time = (job.tasks || []).reduce((maxOverdue, task) => {
71
69
  return (task.overdue_time && task.overdue_time > maxOverdue) ? task.overdue_time : maxOverdue;
72
70
  }, 0);
@@ -81,8 +79,7 @@ AvroQueryClient.prototype.useGetJob = function (jobId) {
81
79
  queryKey: ['job', jobId],
82
80
  queryFn: async () => {
83
81
  const job = await this.get(`/job/${jobId}`);
84
- job.tasks = job.tasks.map((task) => new Task(task));
85
- return job;
82
+ return new Job(job);
86
83
  },
87
84
  enabled: Boolean(jobId),
88
85
  });
@@ -1,11 +1,13 @@
1
1
  import { useMutation, useQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../../client/QueryClient';
3
+ import { Route } from '../../types/api';
3
4
  AvroQueryClient.prototype.useGetRoutes = function (body, total, onProgress) {
4
5
  return useQuery({
5
6
  queryKey: ['routes', this.companyId, body.amt ?? 50, body.query ?? "", total ?? "all"],
6
7
  queryFn: async () => {
7
8
  if (typeof total !== "number") {
8
- return this.fetchRoutes({ ...body, offset: 0 });
9
+ const routes = await this.fetchRoutes({ ...body, offset: 0 });
10
+ return routes.map((route) => new Route(route));
9
11
  }
10
12
  onProgress?.(0);
11
13
  const pageCount = body.amt ? Math.ceil(total / body.amt) + 1 : 1;
@@ -21,7 +23,7 @@ AvroQueryClient.prototype.useGetRoutes = function (body, total, onProgress) {
21
23
  return result;
22
24
  }));
23
25
  const pages = await Promise.all(trackedPromises);
24
- const routes = pages.flat();
26
+ const routes = pages.flat().map((route) => new Route(route));
25
27
  return routes;
26
28
  },
27
29
  });
@@ -30,7 +32,10 @@ AvroQueryClient.prototype.useGetRoute = function (routeId) {
30
32
  const queryClient = this.getQueryClient();
31
33
  return useQuery({
32
34
  queryKey: ['route', routeId],
33
- queryFn: () => this.get(`/route/${routeId}`),
35
+ queryFn: async () => {
36
+ const route = await this.get(`/route/${routeId}`);
37
+ return new Route(route);
38
+ },
34
39
  enabled: Boolean(routeId) && routeId.length > 0,
35
40
  });
36
41
  };
@@ -50,13 +55,13 @@ AvroQueryClient.prototype.useCreateRoute = function () {
50
55
  return {
51
56
  ...oldData,
52
57
  pages: [
53
- [{ ...routeData, id: 'temp-id' }],
58
+ [new Route({ ...routeData, id: 'temp-id' })],
54
59
  ...oldData.pages,
55
60
  ],
56
61
  };
57
62
  }
58
63
  if (Array.isArray(oldData)) {
59
- return [{ ...routeData, id: 'temp-id' }, ...oldData];
64
+ return [new Route({ ...routeData, id: 'temp-id' }), ...oldData];
60
65
  }
61
66
  return oldData;
62
67
  });
@@ -93,18 +98,18 @@ AvroQueryClient.prototype.useUpdateRoute = function () {
93
98
  await queryClient.cancelQueries({ queryKey: ['routes'] });
94
99
  const previousRoute = queryClient.getQueryData(['route', routeId]);
95
100
  const previousRoutes = queryClient.getQueryData(['routes']);
96
- queryClient.setQueryData(['route', routeId], (oldData) => oldData ? { ...oldData, ...updates } : undefined);
101
+ queryClient.setQueryData(['route', routeId], (oldData) => oldData ? new Route({ ...oldData, ...updates }) : undefined);
97
102
  queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
98
103
  if (!oldData)
99
104
  return oldData;
100
105
  if (oldData.pages) {
101
106
  return {
102
107
  ...oldData,
103
- pages: oldData.pages.map((page) => page.map((route) => route.id === routeId ? { ...route, ...updates } : route)),
108
+ pages: oldData.pages.map((page) => page.map((route) => route.id === routeId ? new Route({ ...route, ...updates }) : route)),
104
109
  };
105
110
  }
106
111
  if (Array.isArray(oldData)) {
107
- return oldData.map((route) => route.id === routeId ? { ...route, ...updates } : route);
112
+ return oldData.map((route) => route.id === routeId ? new Route({ ...route, ...updates }) : route);
108
113
  }
109
114
  return oldData;
110
115
  });
@@ -2,28 +2,35 @@ import { Task } from "../../types/api/Task";
2
2
  import { _Event } from "../../types/api/_Event";
3
3
  import { RouteJob } from "../../types/api/RouteJob";
4
4
  import { Subscription } from "../../types/api/Subscription";
5
- export interface Job {
6
- address: string;
7
- company: string;
8
- description: string;
9
- id: string;
10
- is_one_time: boolean;
11
- autostart_radius: number;
12
- latitude: number;
13
- longitude: number;
14
- name: string;
15
- internal_notes: string;
16
- external_notes: string;
17
- priority: number;
18
- tasks: Partial<Task>[];
19
- time_created: number;
20
- time_updated: number | null;
21
- routes: RouteJob[];
22
- subscribers: Subscription[];
23
- manual_emails: string[][];
24
- overdue_time: number;
25
- last_completed_event: _Event | null;
26
- current_event: _Event | null;
27
- labels: string[];
28
- owner: string;
5
+ declare module '../../types/api/Job' {
6
+ interface Job {
7
+ address: string;
8
+ company: string;
9
+ description: string;
10
+ id: string;
11
+ is_one_time: boolean;
12
+ autostart_radius: number;
13
+ latitude: number;
14
+ longitude: number;
15
+ name: string;
16
+ internal_notes: string;
17
+ external_notes: string;
18
+ priority: number;
19
+ tasks: Partial<Task>[];
20
+ time_created: number;
21
+ time_updated: number | null;
22
+ routes: RouteJob[];
23
+ subscribers: Subscription[];
24
+ manual_emails: string[][];
25
+ overdue_time: number;
26
+ last_completed_event: _Event | null;
27
+ current_event: _Event | null;
28
+ labels: string[];
29
+ owner: string;
30
+ }
31
+ }
32
+ export declare class Job {
33
+ constructor(init?: Partial<Job>);
34
+ portionDone: () => number;
35
+ isDone: () => boolean;
29
36
  }
@@ -1 +1,22 @@
1
- export {};
1
+ import { Task } from "../../types/api/Task";
2
+ import { RouteJob } from "../../types/api/RouteJob";
3
+ export class Job {
4
+ constructor(init) {
5
+ this.portionDone = () => {
6
+ if (!this.tasks || this.tasks.length === 0) {
7
+ return 0;
8
+ }
9
+ return this.tasks.filter(t => t.isDone?.()).length / this.tasks.length;
10
+ };
11
+ this.isDone = () => {
12
+ return this.portionDone() === 1;
13
+ };
14
+ Object.assign(this, init);
15
+ if (init?.tasks) {
16
+ this.tasks = init.tasks.map(t => new Task(t));
17
+ }
18
+ if (init?.routes) {
19
+ this.routes = init.routes.map(r => new RouteJob(r));
20
+ }
21
+ }
22
+ }
@@ -53,4 +53,5 @@ export declare class Task {
53
53
  computeNextTaskState: (isCustomer: boolean, action: "accept" | "modify" | "reject") => TaskStatus;
54
54
  getPrepaidEventsRemaining: () => number;
55
55
  getPrepaidMonthsRemaining: () => number;
56
+ isDone: (fallback?: number) => number | boolean;
56
57
  }
@@ -51,6 +51,18 @@ export class Task {
51
51
  return acc;
52
52
  }, 0);
53
53
  };
54
+ this.isDone = (fallback = 0) => {
55
+ if (!this.status || this.status !== TaskStatus.ACTIVE) {
56
+ return 1;
57
+ }
58
+ if (this.frequency > 0) {
59
+ return this.overdue_time <= 0;
60
+ }
61
+ else {
62
+ // If frequency is 0, task falls back to company.indicator_lifetime
63
+ return this.last_completed_event?.time_ended ?? 0 > (new Date().getTime() / 1000 - fallback);
64
+ }
65
+ };
54
66
  Object.assign(this, init);
55
67
  if (init?.prepayments) {
56
68
  this.prepayments = init.prepayments.map(p => new Prepayment(p));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-avro/avro-js",
3
- "version": "0.0.4-beta.20",
3
+ "version": "0.0.4-beta.22",
4
4
  "description": "JS client for Avro backend integration.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",