@ray-js/lock-sdk 1.1.0 → 1.1.1-beta.10

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.
package/lib/user.js CHANGED
@@ -1,4 +1,4 @@
1
- import { getUserDetail, getUserList } from "./api/user";
1
+ import { getUserDetail, getUserDetailQuery, getUserList, } from "./api/user";
2
2
  import config from "./config";
3
3
  import { update as updateMap, reportUpdate as reportUpdateMap, } from "./config/dp-map/unlock-method";
4
4
  import { update as updateMapBig, reportUpdate as reportUpdateMapBig, } from "./config/dp-map/unlock-method-big";
@@ -8,62 +8,67 @@ import DpUtils from "@ray-js/tuya-dp-transform";
8
8
  import { getError } from "./utils/errors";
9
9
  import { updateUserTimeSchedule, addNormalUser, removeNormalUser, } from "./api/user";
10
10
  import { validConfigDpMap } from "./config/dp-map/common";
11
- import { formatTimestampToMilliseconds, formatWeek, isAdmin, parseWeek, unlockMethodConfigs, validateEffectiveConfig, } from "./utils";
11
+ import { formatTimestampToMilliseconds, formatWeek, getPermanentSetting, isAdmin, isUseNearChannel, parseWeek, unlockMethodConfigs, validateEffectiveConfig, } from "./utils";
12
12
  import { getCurrentHomeInfo, isSupportShortLink } from "./utils/device";
13
13
  import { LoopTypes } from "./utils/constant";
14
14
  import { getCurrentUser } from "./state";
15
- const concactUserList = (list, result) => {
16
- return list.reduce((acc, item) => {
17
- const { timeScheduleInfo, unlockDetail, ...rest } = item;
18
- let scheduleInfo = {
19
- permanent: true,
20
- };
21
- if (!item.timeScheduleInfo.permanent &&
22
- item.timeScheduleInfo.scheduleDetails &&
23
- item.timeScheduleInfo.scheduleDetails.length > 0) {
24
- Object.assign(timeScheduleInfo, {
25
- effectiveTime: formatTimestampToMilliseconds(item.timeScheduleInfo.effectiveTime || 0),
26
- expiredTime: formatTimestampToMilliseconds(item.timeScheduleInfo.expiredTime || 0),
27
- scheduleDetails: {
28
- repeat: !item.timeScheduleInfo.scheduleDetails[0].allDay,
29
- effectiveTime: item.timeScheduleInfo.scheduleDetails[0].effectiveTime,
30
- invalidTime: item.timeScheduleInfo.scheduleDetails[0].invalidTime,
31
- timeZoneId: item.timeScheduleInfo.scheduleDetails[0].timeZoneId,
32
- weeks: parseWeek(item.timeScheduleInfo.scheduleDetails[0].workingDay),
33
- },
34
- });
35
- }
36
- let unlockDetails = unlockDetail.map((detail) => {
37
- const unlockList = detail.unlockList.map((unlockItem) => {
38
- return {
39
- unlockId: unlockItem.unlockId.split("-")[1],
40
- unlockName: unlockItem.unlockName,
41
- id: unlockItem.opModeId,
42
- isBound: unlockItem.allocateFlag === 1,
43
- photoUnlock: unlockItem.photoUnlock,
44
- isSpecial: unlockItem.unlockAttr === 1,
45
- };
46
- });
47
- const unlockMethodConfig = unlockMethodConfigs.find((item) => item.code === detail.dpCode);
15
+ import { deleteUser } from "./utils/user";
16
+ import { UserType } from "./constant";
17
+ const handleUserDetail = (user) => {
18
+ const { timeScheduleInfo, unlockDetail, ...rest } = user;
19
+ let scheduleInfo = {
20
+ permanent: true,
21
+ };
22
+ if (!timeScheduleInfo.permanent &&
23
+ timeScheduleInfo.scheduleDetails &&
24
+ timeScheduleInfo.scheduleDetails.length > 0) {
25
+ Object.assign(scheduleInfo, {
26
+ permanent: false,
27
+ effectiveTime: formatTimestampToMilliseconds(timeScheduleInfo.effectiveTime || 0),
28
+ expiredTime: formatTimestampToMilliseconds(timeScheduleInfo.expiredTime || 0),
29
+ scheduleDetails: {
30
+ repeat: !timeScheduleInfo.scheduleDetails[0].allDay,
31
+ effectiveTime: timeScheduleInfo.scheduleDetails[0].effectiveTime,
32
+ invalidTime: timeScheduleInfo.scheduleDetails[0].invalidTime,
33
+ timeZoneId: timeScheduleInfo.scheduleDetails[0].timeZoneId,
34
+ weeks: parseWeek(timeScheduleInfo.scheduleDetails[0].workingDay),
35
+ },
36
+ });
37
+ }
38
+ let unlockDetails = unlockDetail.map((detail) => {
39
+ const unlockList = detail.unlockList.map((unlockItem) => {
48
40
  return {
49
- count: unlockList.length,
50
- dpCode: detail.dpCode,
51
- dpId: detail.dpId,
52
- type: unlockMethodConfig.type,
53
- unlockList,
41
+ unlockId: +unlockItem.unlockId.split("-")[1],
42
+ unlockName: unlockItem.unlockName,
43
+ id: unlockItem.opModeId,
44
+ isBound: unlockItem.allocateFlag === 1,
45
+ photoUnlock: unlockItem.photoUnlock,
46
+ isSpecial: unlockItem.unlockAttr === 1,
54
47
  };
55
48
  });
56
- acc.push({
57
- ...rest,
58
- timeScheduleInfo: scheduleInfo,
59
- unlockDetails,
60
- isAccountUser: item.userListType === "group_user",
61
- });
49
+ const unlockMethodConfig = unlockMethodConfigs.find((item) => item.code === detail.dpCode);
50
+ return {
51
+ count: unlockList.length,
52
+ dpCode: detail.dpCode,
53
+ dpId: detail.dpId,
54
+ type: unlockMethodConfig.type,
55
+ unlockList,
56
+ };
57
+ });
58
+ return {
59
+ ...rest,
60
+ timeScheduleInfo: scheduleInfo,
61
+ unlockDetails,
62
+ isAccountUser: user.userListType === "group_user",
63
+ };
64
+ };
65
+ const concactUserList = (list, result) => {
66
+ return list.reduce((acc, item) => {
67
+ acc.push(handleUserDetail(item));
62
68
  return acc;
63
69
  }, result);
64
70
  };
65
- export const getUsers = async (params) => {
66
- const { page = 1, pageSize = 10, keyword = "" } = params || {};
71
+ const getUserDpCodes = async () => {
67
72
  const currentUser = await getCurrentUser();
68
73
  const dps = [];
69
74
  if (currentUser.allOpenDps) {
@@ -75,6 +80,11 @@ export const getUsers = async (params) => {
75
80
  return acc;
76
81
  }, dps);
77
82
  }
83
+ return dps;
84
+ };
85
+ export const getUsers = async (params) => {
86
+ const { page = 1, pageSize = 10, keyword = "" } = params || {};
87
+ const dps = await getUserDpCodes();
78
88
  const res = await getUserList({
79
89
  dpCodes: dps.join(","),
80
90
  devId: config.devInfo.devId,
@@ -95,18 +105,13 @@ export const getUsers = async (params) => {
95
105
  };
96
106
  };
97
107
  export const getUserInfo = async (params) => {
98
- const user = await getUserDetail({
108
+ const currentUser = await getCurrentUser();
109
+ const res = await getUserDetailQuery({
99
110
  devId: config.devInfo.devId,
100
111
  userId: params.userId,
112
+ dpIds: currentUser.allOpenDps,
101
113
  });
102
- const data = await getUsers({
103
- keyword: user.nickName,
104
- });
105
- const res = data.list.find((item) => item.userId === params.userId);
106
- if (res) {
107
- return res;
108
- }
109
- throw getError(1048);
114
+ return handleUserDetail(res);
110
115
  };
111
116
  export const updateUserLimitTime = async (params) => {
112
117
  const { userId, lockUserId, permanent, effective } = params;
@@ -134,52 +139,50 @@ export const updateUserLimitTime = async (params) => {
134
139
  validNum: 0,
135
140
  pwdLength: 0,
136
141
  pwd: [],
137
- validConfig: {
138
- startTime: Math.floor(permanent ? +new Date() / 1000 : effective.effectiveDate),
139
- endTime: Math.floor(permanent
140
- ? (+new Date() + 3153600000000) / 1000
141
- : effective.expiredDate),
142
- loop: permanent ? LoopTypes.NONE : loopType,
143
- loopConfig: 0,
144
- weeks: permanent ||
145
- effective?.repeat !== "week" ||
146
- effective?.weeks === undefined
147
- ? [0, 0, 0, 0, 0, 0, 0]
148
- : effective.weeks,
149
- days: [],
150
- startHour: permanent ? 0 : Math.floor(effectiveTime / 60),
151
- startMinute: permanent ? 0 : effectiveTime % 60,
152
- endHour: permanent ? 0 : Math.floor(expiredTime / 60),
153
- endMinute: permanent ? 0 : expiredTime % 60,
154
- },
142
+ validConfig: permanent
143
+ ? getPermanentSetting()
144
+ : {
145
+ startTime: effective.effectiveDate,
146
+ endTime: effective.expiredDate,
147
+ loop: loopType,
148
+ loopConfig: 0,
149
+ weeks: effective?.repeat !== "week" || effective?.weeks === undefined
150
+ ? [0, 0, 0, 0, 0, 0, 0]
151
+ : effective.weeks,
152
+ days: [],
153
+ startHour: Math.floor(effectiveTime / 60),
154
+ startMinute: effectiveTime % 60,
155
+ endHour: Math.floor(expiredTime / 60),
156
+ endMinute: expiredTime % 60,
157
+ },
155
158
  };
156
- const dpCode = config.supportBigData
157
- ? dpCodes.unlockMethodEditW
158
- : dpCodes.unlockMethodEdit;
159
- const res = (await publishDps({
160
- [dpCode]: DpUtils.format(dpValue, (config.supportBigData ? updateMapBig : updateMap)),
161
- }, {
162
- checkReport: (dps) => {
163
- if (typeof dps[dpCode] !== "undefined") {
164
- const result = DpUtils.parse(dps[dpCode], (config.supportBigData
165
- ? reportUpdateMapBig
166
- : reportUpdateMap));
167
- if (result.type === 0) {
168
- if (result.memberId === lockUserId) {
169
- return result;
170
- }
171
- else {
172
- console.warn("An incorrect member id was returned");
159
+ try {
160
+ const dpCode = config.supportBigData
161
+ ? dpCodes.unlockMethodEditW
162
+ : dpCodes.unlockMethodEdit;
163
+ const res = (await publishDps({
164
+ [dpCode]: DpUtils.format(dpValue, (config.supportBigData ? updateMapBig : updateMap)),
165
+ }, {
166
+ checkReport: (dps) => {
167
+ if (typeof dps[dpCode] !== "undefined") {
168
+ const result = DpUtils.parse(dps[dpCode], (config.supportBigData
169
+ ? reportUpdateMapBig
170
+ : reportUpdateMap));
171
+ if (result.type === 0) {
172
+ if (result.memberId === lockUserId) {
173
+ return result;
174
+ }
175
+ else {
176
+ console.warn("An incorrect member id was returned");
177
+ }
173
178
  }
174
179
  }
175
- }
176
- return false;
177
- },
178
- }));
179
- if (res.status !== 255) {
180
- throw getError(1011);
181
- }
182
- try {
180
+ return false;
181
+ },
182
+ }));
183
+ if (res.status !== 255) {
184
+ throw getError(1011);
185
+ }
183
186
  await updateUserTimeSchedule({
184
187
  devId: config.devInfo.devId,
185
188
  userId: userId,
@@ -241,11 +244,8 @@ export const openFamilyUserDetail = async (userId) => {
241
244
  const url = `tuyaSmart://member_info?homeId=${homeId}&memberId=${userId}&role=${role}`;
242
245
  ty.router({
243
246
  url: url,
244
- success: (d) => {
245
- console.log("openFamilyUserDetail success");
246
- },
247
247
  fail: (e) => {
248
- console.log("openFamilyUserDetail fail", e);
248
+ console.error("openFamilyUserDetail fail", e);
249
249
  },
250
250
  });
251
251
  return;
@@ -268,6 +268,17 @@ export const removeUser = async (userId) => {
268
268
  if (!config.supportBigData) {
269
269
  throw getError(1060);
270
270
  }
271
+ const res = await getUserDetail({
272
+ devId: config.devInfo.devId,
273
+ userId,
274
+ });
275
+ if (isUseNearChannel()) {
276
+ return deleteUser({
277
+ userId,
278
+ lockUserId: res.lockUserId,
279
+ admin: res.userType === UserType.ADMIN || res.userType === UserType.OWNER,
280
+ });
281
+ }
271
282
  return removeNormalUser({
272
283
  devId: config.devInfo.devId,
273
284
  userId,
@@ -5,6 +5,7 @@ export declare const setDeviceProperty: (devId: string, code: string, value: str
5
5
  export declare const publishDeviceDps: (params: {
6
6
  deviceId: string;
7
7
  dps: Record<string, boolean | number | string>;
8
+ pipelines?: number[];
8
9
  }) => Promise<unknown>;
9
10
  export declare const registerDevice: (devIds: string[]) => void;
10
11
  export declare const registerBleStatus: (devId: string) => void;
@@ -131,7 +131,7 @@ export const requestCloud = (options) => {
131
131
  resolve(d);
132
132
  },
133
133
  fail: (err) => {
134
- reject(err.innerError ?? err);
134
+ reject(err ?? err);
135
135
  },
136
136
  });
137
137
  });
@@ -63,6 +63,9 @@ const errors = {
63
63
  1062: "Device does not support active connection",
64
64
  1063: "Password length only supports 4-6 digits",
65
65
  1064: "Non-thread device, not supported in LAN connection to open and close locks",
66
+ 1065: "Cloud side has been deleted, but device side synchronization deletion failed",
67
+ 1066: "Phone number cannot be empty",
68
+ 1067: "Password limit reached within this period",
66
69
  };
67
70
  export const getError = (code, ...places) => {
68
71
  if (errors[code]) {
@@ -107,7 +107,7 @@ export declare const parseOfflinePassword: (value: string) => string;
107
107
  export declare const formatTimestampToMilliseconds: (timestamp: number) => number;
108
108
  export declare const unlockMethodConfigs: UnlockMethodConfig[];
109
109
  export declare const getUnlockMethodTypeByDpCode: (dpCode: string) => UnlockMethodConfig;
110
- export declare const getUnlockMethodTypeById: (id: number) => UnlockMethodConfig;
110
+ export declare const getUnlockMethodTypeById: (id: number | string) => UnlockMethodConfig;
111
111
  export declare const getUnlockMethodTypeByType: (type: string) => UnlockMethodConfig;
112
112
  export declare const parallelMerge: <T extends (...args: any[]) => Promise<any>>(cb: T) => (...args: Parameters<T>) => ReturnType<T>;
113
113
  export declare const parallelOnly: <T extends (...args: any[]) => Promise<any>>(cb: T) => (...args: Parameters<T>) => ReturnType<T>;
@@ -49,7 +49,8 @@ export function getCapabilities(capability) {
49
49
  });
50
50
  }
51
51
  export const formatWeek = (value) => {
52
- return parseInt([...value].reverse().join(""), 2);
52
+ const temp = value.slice(0, 7);
53
+ return parseInt(temp.reverse().join(""), 2);
53
54
  };
54
55
  export const parseWeek = (value) => {
55
56
  return Number(value)
@@ -64,7 +65,7 @@ export const getPermanentSetting = () => {
64
65
  const now = Math.floor(+new Date() / 1000);
65
66
  return {
66
67
  startTime: now,
67
- endTime: now + 3153600000,
68
+ endTime: 0x72bc9b7f,
68
69
  loop: LoopTypes.NONE,
69
70
  loopConfig: 0,
70
71
  weeks: [0, 0, 0, 0, 0, 0, 0],
@@ -258,18 +259,20 @@ onBackgroundFetchData(preFetchKeys, (res) => {
258
259
  });
259
260
  });
260
261
  export const getDataWithPreFetch = async (key, apiCb) => {
261
- if (preFetchData[key]) {
262
- return preFetchData[key];
263
- }
264
- try {
265
- const result = await getBackgroundFetchData([key]);
266
- if (result &&
267
- result.fetchedData[key] &&
268
- result.fetchedData[key].fetchedData) {
269
- return result.fetchedData[key].fetchedData;
262
+ if (config.preFetch) {
263
+ if (preFetchData[key]) {
264
+ return preFetchData[key];
265
+ }
266
+ try {
267
+ const result = await getBackgroundFetchData([key]);
268
+ if (result &&
269
+ result.fetchedData[key] &&
270
+ result.fetchedData[key].fetchedData) {
271
+ return result.fetchedData[key].fetchedData;
272
+ }
273
+ }
274
+ catch {
270
275
  }
271
- }
272
- catch {
273
276
  }
274
277
  return apiCb();
275
278
  };
@@ -7,7 +7,7 @@ import { getError } from "./errors";
7
7
  export const publishDpsOnly = async (dpData) => {
8
8
  const { devInfo, idsByCode } = config;
9
9
  const option = {};
10
- if (config.onlineType === 0) {
10
+ if (config.deviceStatus.type === "offline") {
11
11
  throw getError(1001);
12
12
  }
13
13
  if (isOnlineByType(config.onlineType, 2)) {
@@ -0,0 +1,2 @@
1
+ import { RemovedUserData } from "../api/user";
2
+ export declare const deleteUser: (data: RemovedUserData) => Promise<void>;
@@ -0,0 +1,39 @@
1
+ import { removeUser } from "../api/user";
2
+ import dpCodes from "../config/dp-code";
3
+ import dpUtils from "@ray-js/tuya-dp-transform";
4
+ import { remove as removeBigMap, reportRemove as reportRemoveBigMap, } from "../config/dp-map/unlock-method-big";
5
+ import { remove as removeMap, reportRemove as reportRemoveMap, } from "../config/dp-map/unlock-method";
6
+ import { publishDps } from "../utils/publishDps";
7
+ import config from "../config";
8
+ export const deleteUser = async (data) => {
9
+ const { admin, lockUserId, userId } = data;
10
+ const dpCode = config.supportBigData
11
+ ? dpCodes.unlockMethodDelW
12
+ : dpCodes.unlockMethodDel;
13
+ const dpData = {
14
+ type: 0,
15
+ stage: 0,
16
+ admin: !!admin,
17
+ memberId: lockUserId,
18
+ unlockId: config.supportBigData ? 0xffff : 0xff,
19
+ category: 0,
20
+ };
21
+ await publishDps({
22
+ [dpCode]: dpUtils.format(dpData, (config.supportBigData ? removeBigMap : removeMap)),
23
+ }, {
24
+ checkReport: (dps) => {
25
+ if (dps[dpCode]) {
26
+ const dpValue = dpUtils.parse(dps[dpCode], (config.supportBigData
27
+ ? reportRemoveBigMap
28
+ : reportRemoveMap));
29
+ if (dpValue.type === 0 &&
30
+ dpValue.status == 255 &&
31
+ dpValue.memberId === lockUserId) {
32
+ return true;
33
+ }
34
+ }
35
+ return false;
36
+ },
37
+ });
38
+ await removeUser(config.devInfo.devId, userId);
39
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/lock-sdk",
3
- "version": "1.1.0",
3
+ "version": "1.1.1-beta.10",
4
4
  "files": [
5
5
  "lib",
6
6
  "LICENSE.md"
@@ -1,7 +0,0 @@
1
- export interface UpdateUserEffectiveData {
2
- type: 0;
3
- stage: number;
4
- admin: boolean;
5
- memberId: number;
6
- unlockId: 255;
7
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- export declare const syncUnlockMethod: () => Promise<void>;
2
- export declare const autoSyncUnlockMethod: () => Promise<void>;
@@ -1,42 +0,0 @@
1
- import { fetchSyncUnlockMethodInfo } from "../api/lock";
2
- import config from "../config";
3
- import dpCodes from "../config/dp-code";
4
- import { getCurrentUser } from "../state";
5
- import { publishDps } from "../utils/publishDps";
6
- import dpUtils from "@ray-js/tuya-dp-transform";
7
- import { reportSync as reportSyncMap } from "../config/dp-map/unlock-method";
8
- import { reportSync as reportSyncBigMap } from "../config/dp-map/unlock-method-big";
9
- let syncUnlockMethodFirst = true;
10
- export const syncUnlockMethod = async () => {
11
- syncUnlockMethodFirst = false;
12
- const { allOpenDps = "" } = await getCurrentUser();
13
- const { ins = "", distributed = false } = await fetchSyncUnlockMethodInfo({
14
- devId: config.devInfo.devId,
15
- dpIds: allOpenDps.split(","),
16
- });
17
- if (ins && distributed) {
18
- const dpCode = config.supportBigData
19
- ? dpCodes.synchMethodW
20
- : dpCodes.synchMethod;
21
- await publishDps({ [dpCode]: ins }, {
22
- checkReport: (dpData) => {
23
- if (typeof dpData[dpCode] !== "undefined") {
24
- const result = dpUtils.parse(dpData[dpCode], config.supportBigData ? reportSyncBigMap : reportSyncMap);
25
- if (result.stage === 1) {
26
- return true;
27
- }
28
- }
29
- return false;
30
- },
31
- });
32
- }
33
- };
34
- export const autoSyncUnlockMethod = async () => {
35
- try {
36
- if (syncUnlockMethodFirst) {
37
- await syncUnlockMethod();
38
- }
39
- }
40
- catch (e) {
41
- }
42
- };