@haiiro/docuflow 0.0.11 → 0.0.12

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.
@@ -4,8 +4,10 @@ import { type RoutePayload } from 'twenty-sdk/logic-function';
4
4
  import { restAnd, restEq, restIsNull, restNeq, restOr } from 'src/shared/api/rest-filter.util';
5
5
  import { readRestList } from 'src/shared/api/rest-response.util';
6
6
  import { BitrixError, callBitrix, isBitrixConfigured } from 'src/shared/bitrix/bitrix-client';
7
+ import { type BitrixUserProfile } from 'src/shared/bitrix/bitrix-user.util';
7
8
  import { parseBitrixRequest } from 'src/shared/bitrix/bitrix.util';
8
9
  import { DOCFLOW_SECTION, TASK_DONE_STATUS } from 'src/shared/docflow.constants';
10
+ import { findOrCreateWorkspaceMemberFromBitrix } from 'src/server/bitrix-member';
9
11
  import { proveAndLinkBitrixPortal } from 'src/server/bitrix-portal';
10
12
  import { renderBitrixPage, type BitrixPageTask } from 'src/server/bitrix-page';
11
13
  import { htmlResponse } from 'src/server/http-response';
@@ -22,28 +24,16 @@ const getCrmUrl = (): string | undefined => {
22
24
  : undefined;
23
25
  };
24
26
 
25
- // The person is found by the e-mail of their Bitrix24 account; their open
26
- // docflow tasks are listed with links into the CRM.
27
- const findMyTasks = async (
28
- email: string,
27
+ const listOpenTasksForMember = async (
28
+ memberId: string,
29
29
  crmUrl: string | undefined,
30
- ): Promise<BitrixPageTask[] | undefined> => {
31
- const [member] = readRestList<{ id: string }>(
32
- await restClient('application').get('/rest/workspaceMembers', {
33
- query: { filter: restEq('userEmail', email), limit: 1 },
34
- }),
35
- ).records;
36
-
37
- if (member === undefined) {
38
- return undefined;
39
- }
40
-
41
- return readRestList<{ id: string; title?: string | null; dueAt?: string | null }>(
30
+ ): Promise<BitrixPageTask[]> =>
31
+ readRestList<{ id: string; title?: string | null; dueAt?: string | null }>(
42
32
  await restClient('application').get('/rest/tasks', {
43
33
  query: {
44
34
  filter: restAnd(
45
35
  restEq('smbSection', DOCFLOW_SECTION),
46
- restEq('assigneeId', member.id),
36
+ restEq('assigneeId', memberId),
47
37
  restOr(restNeq('status', TASK_DONE_STATUS), restIsNull('status')),
48
38
  ),
49
39
  order_by: 'dueAt[AscNullsLast]',
@@ -58,7 +48,6 @@ const findMyTasks = async (
58
48
  ? new Date(task.dueAt).toLocaleDateString('ru-RU')
59
49
  : undefined,
60
50
  }));
61
- };
62
51
 
63
52
  // "Путь вашего обработчика" of the local Bitrix24 application: the page a
64
53
  // person sees when they open the app in the portal.
@@ -86,41 +75,61 @@ const handler = async (event: RoutePayload) => {
86
75
 
87
76
  try {
88
77
  const portal = await proveAndLinkBitrixPortal(request);
89
- const user = await callBitrix<{ EMAIL?: string }>({
78
+ const user = await callBitrix<BitrixUserProfile>({
90
79
  restEndpoint: portal.restEndpoint,
91
80
  accessToken: request.authId ?? portal.accessToken,
92
81
  method: 'user.current',
93
82
  params: {},
94
83
  });
95
- const tasks =
96
- typeof user.EMAIL === 'string' && user.EMAIL.length > 0
97
- ? await findMyTasks(user.EMAIL, crmUrl)
98
- : undefined;
84
+ const member = await findOrCreateWorkspaceMemberFromBitrix(user);
85
+
86
+ if (member === undefined) {
87
+ return htmlResponse(
88
+ 200,
89
+ renderBitrixPage({
90
+ title: 'Документооборот',
91
+ message: 'В профиле Битрикс24 нет почты — без неё нельзя найти или создать сотрудника в CRM.',
92
+ crmUrl,
93
+ tasks: [],
94
+ isInstall: false,
95
+ }),
96
+ );
97
+ }
98
+
99
+ const tasks = await listOpenTasksForMember(member.id, crmUrl);
99
100
 
100
101
  return htmlResponse(
101
102
  200,
102
103
  renderBitrixPage({
103
104
  title: 'Документооборот',
104
- message:
105
- tasks === undefined
106
- ? 'В CRM нет сотрудника с почтой вашего профиля Битрикс24.'
107
- : tasks.length === 0
108
- ? 'Открытых заданий нет.'
109
- : 'Ваши открытые задания:',
105
+ message: tasks.length === 0 ? 'Открытых заданий нет.' : 'Ваши открытые задания:',
110
106
  crmUrl,
111
- tasks: tasks ?? [],
107
+ tasks,
112
108
  isInstall: false,
113
109
  }),
114
110
  );
115
111
  } catch (error) {
112
+ if (error instanceof BitrixError) {
113
+ return htmlResponse(
114
+ 403,
115
+ renderBitrixPage({
116
+ title: 'Документооборот',
117
+ message: `Запрос портала не подтверждён: ${error.message}`,
118
+ crmUrl,
119
+ tasks: [],
120
+ isInstall: false,
121
+ }),
122
+ );
123
+ }
124
+
116
125
  return htmlResponse(
117
- 403,
126
+ 200,
118
127
  renderBitrixPage({
119
128
  title: 'Документооборот',
120
129
  message:
121
- error instanceof BitrixError
122
- ? `Запрос портала не подтверждён: ${error.message}`
123
- : 'Запрос портала не подтверждён.',
130
+ error instanceof Error
131
+ ? error.message
132
+ : 'Не удалось открыть документооборот для вашего профиля Битрикс24.',
124
133
  crmUrl,
125
134
  tasks: [],
126
135
  isInstall: false,
@@ -42,7 +42,8 @@ var readRestList = (response) => {
42
42
  const value = readDataValue(response);
43
43
  return {
44
44
  records: Array.isArray(value) ? value : [],
45
- pageInfo: response?.pageInfo ?? {}
45
+ pageInfo: response?.pageInfo ?? {},
46
+ totalCount: response?.totalCount
46
47
  };
47
48
  };
48
49
  var readRestRecord = (response) => {
@@ -59551,7 +59551,8 @@ var readRestList = (response) => {
59551
59551
  const value = readDataValue(response);
59552
59552
  return {
59553
59553
  records: Array.isArray(value) ? value : [],
59554
- pageInfo: response?.pageInfo ?? {}
59554
+ pageInfo: response?.pageInfo ?? {},
59555
+ totalCount: response?.totalCount
59555
59556
  };
59556
59557
  };
59557
59558
  var readRestRecord = (response) => {
@@ -119461,7 +119461,8 @@ var readRestList = (response) => {
119461
119461
  const value = readDataValue(response);
119462
119462
  return {
119463
119463
  records: Array.isArray(value) ? value : [],
119464
- pageInfo: response?.pageInfo ?? {}
119464
+ pageInfo: response?.pageInfo ?? {},
119465
+ totalCount: response?.totalCount
119465
119466
  };
119466
119467
  };
119467
119468
  var readRestRecord = (response) => {