UNPKG

@dodi-smart/ttlock-graphql-api

Version:
506 lines (503 loc) 24.7 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const builder_1 = require("../builder"); const logger_1 = require("../logger"); const session_1 = require("../session"); const utils_1 = require("../utils"); const ttlock_api_1 = require("@dodi-smart/ttlock-api"); const graphql_1 = require("graphql"); builder_1.builder.objectType('TTLock', { fields: (t) => ({ smartlocks: t.field({ type: ['TTLockSmartlock'], args: { limit: t.arg.int({ description: 'A limit on the number of objects to be returned. Limit can range between 20 and 10_000, and the default is 20.', required: false, defaultValue: 20 }), offset: t.arg.int({ description: 'Page offset starting from 1', required: false, defaultValue: 1 }) }, resolve: (_parent, { limit, offset }, context) => __awaiter(void 0, void 0, void 0, function* () { var _a; const { isAllowed } = context; if (!(yield isAllowed(context))) { throw new graphql_1.GraphQLError('Not allowed'); } const { clientId } = session_1.authHolder; const { accessToken } = session_1.authHolder.auth; if (!accessToken) { logger_1.logger.error("Failed executing 'smartlocks': Missing accessToken"); return []; } const lockClient = new ttlock_api_1.LockApi(); const smartlocks = yield lockClient.getLocks({ clientId, accessToken, date: new Date().getTime(), pageNo: (0, utils_1.fixNumberNullable)(offset), pageSize: (0, utils_1.fixNumberNullable)(limit) }); if (smartlocks.errcode) { logger_1.logger.error(`Failed executing 'smartlocks': Err(${smartlocks.errcode}): ${smartlocks.errmsg}`); return []; } return (_a = smartlocks.list) !== null && _a !== void 0 ? _a : []; }) }), smartlock: t.field({ nullable: true, type: 'TTLockSmartlockDetails', args: { smartlockId: t.arg.string({ required: true }) }, resolve: (_parent, { smartlockId }, context) => __awaiter(void 0, void 0, void 0, function* () { const { isAllowed } = context; if (!(yield isAllowed(context))) { throw new graphql_1.GraphQLError('Not allowed'); } const { clientId } = session_1.authHolder; const { accessToken } = session_1.authHolder.auth; if (!accessToken) { logger_1.logger.error("Failed executing 'smartlocks': Missing accessToken"); throw new graphql_1.GraphQLError('Not authorized'); } const lockClient = new ttlock_api_1.LockApi(); const smartlock = yield lockClient.getLock({ clientId, accessToken, lockId: +smartlockId, date: new Date().getTime() }); if (smartlock.errcode) { logger_1.logger.error(`Failed executing 'smartlocks': Err(${smartlock.errcode}): ${smartlock.errmsg}`); throw new graphql_1.GraphQLError(`RemoteError(${smartlock.errcode}): ${smartlock.errmsg}`); } return smartlock; }) }) }) }); builder_1.builder.objectType('TTLockMutations', { fields: (t) => ({ lockSmartlock: t.field({ type: 'TTLockResponse', args: { smartlockId: t.arg.string({ required: true }) }, resolve: (_, { smartlockId }, context) => __awaiter(void 0, void 0, void 0, function* () { const { isAllowed } = context; if (!(yield isAllowed(context))) { throw new graphql_1.GraphQLError('Not allowed'); } const { clientId } = session_1.authHolder; const { accessToken } = session_1.authHolder.auth; if (!accessToken) { logger_1.logger.error(`Failed executing 'lockSmartlock': Err(10004): invalid token`); return { errcode: 10004, errmsg: 'invalid token' }; } const gatewayClient = new ttlock_api_1.GatewayApi(); const response = yield gatewayClient.lock({ clientId, accessToken, date: new Date().getTime(), lockId: +smartlockId }); return response; }) }), unlockSmartlock: t.field({ type: 'TTLockResponse', args: { smartlockId: t.arg.string({ required: true }) }, resolve: (_, { smartlockId }, context) => __awaiter(void 0, void 0, void 0, function* () { const { isAllowed } = context; if (!(yield isAllowed(context))) { throw new graphql_1.GraphQLError('Not allowed'); } const { clientId } = session_1.authHolder; const { accessToken } = session_1.authHolder.auth; if (!accessToken) { logger_1.logger.error(`Failed executing 'unlockSmartlock': Err(10004): invalid token`); return { errcode: 10004, errmsg: 'invalid token' }; } const gatewayClient = new ttlock_api_1.GatewayApi(); const response = yield gatewayClient.unlock({ clientId, accessToken, date: new Date().getTime(), lockId: +smartlockId }); return response; }) }), getRandomPasscode: t.field({ type: 'TTLockPasscodeResponse', args: { smartlockId: t.arg.string({ required: true, description: 'Lock ID' }), passcodeType: t.arg.int({ required: true, description: ` One-time 1 This code only valid for once within 6 hours from the Start Time Permanent 2 This code must be used at least once within 24 Hours after the Start Time, Or it will be invalidated Period 3 This code must be used at least once within 24 Hours after the Start Time, Or it will be invalidated Delete 4 This code will delete all other codes Weekend Cyclic 5 This code is valid during the time period at the weekend Daily Cyclic 6 This code is valid during the time period everyday Workday Cyclic 7 This code is valid during the time period on workdays Monday Cyclic 8 This code is valid during the time period on Mondays Tuesday Cyclic 9 This code is valid during the time period on Tuesdays Wednesday Cyclic 10 This code is valid during the time period on Wednesdays Thursday Cyclic 11 This code is valid during the time period on Thursdays Friday Cyclic 12 This code is valid during the time period on Fridays Saturday Cyclic 13 This code is valid during the time period on Saturdays Sunday Cyclic 14 This code is valid during the time period on Sundays ` }), passcodeName: t.arg.string({ required: false, description: 'Passcode name' }), startDate: t.arg.float({ required: true, description: 'The time when it becomes valid (timestamp in millisecond)' }), endDate: t.arg.float({ required: true, description: 'The time when it is expired (timestamp in millisecond)' }) }, resolve: (_, { smartlockId, passcodeType, passcodeName, startDate, endDate }, context) => __awaiter(void 0, void 0, void 0, function* () { const { isAllowed } = context; if (!(yield isAllowed(context))) { throw new graphql_1.GraphQLError('Not allowed'); } const { clientId } = session_1.authHolder; const { accessToken } = session_1.authHolder.auth; if (!accessToken) { logger_1.logger.error(`Failed executing 'getRandomPasscode': Err(10004): invalid token`); return { errcode: 10004, errmsg: 'invalid token' }; } const passcodeClient = new ttlock_api_1.PasscodeApi(); const response = yield passcodeClient.getPasscode({ clientId, accessToken, date: new Date().getTime(), lockId: +smartlockId, keyboardPwdType: passcodeType, keyboardPwdName: (0, utils_1.fixStringNullable)(passcodeName), startDate, endDate: (0, utils_1.fixNumberNullable)(endDate) }); if (response.errcode) { logger_1.logger.error(`Failed executing 'getRandomPasscode': Err(${response.errcode}): ${response.errmsg}`); throw new graphql_1.GraphQLError(`Failed executing 'getRandomPasscode': Err(${response.errcode}): ${response.errmsg}`); } return response; }) }), addPasscode: t.field({ type: 'TTLockPasscodeResponse', args: { smartlockId: t.arg.string({ required: true, description: 'Lock ID' }), passcodeType: t.arg.int({ required: true, description: ` One-time 1 This code only valid for once within 6 hours from the Start Time Permanent 2 This code must be used at least once within 24 Hours after the Start Time, Or it will be invalidated Period 3 This code must be used at least once within 24 Hours after the Start Time, Or it will be invalidated Delete 4 This code will delete all other codes Weekend Cyclic 5 This code is valid during the time period at the weekend Daily Cyclic 6 This code is valid during the time period everyday Workday Cyclic 7 This code is valid during the time period on workdays Monday Cyclic 8 This code is valid during the time period on Mondays Tuesday Cyclic 9 This code is valid during the time period on Tuesdays Wednesday Cyclic 10 This code is valid during the time period on Wednesdays Thursday Cyclic 11 This code is valid during the time period on Thursdays Friday Cyclic 12 This code is valid during the time period on Fridays Saturday Cyclic 13 This code is valid during the time period on Saturdays Sunday Cyclic 14 This code is valid during the time period on Sundays ` }), passcodeName: t.arg.string({ required: false, description: 'Passcode name' }), passcode: t.arg.int({ required: true, description: 'Passcode' }), startDate: t.arg.float({ required: true, description: 'The time when it becomes valid (timestamp in millisecond)' }), endDate: t.arg.float({ required: true, description: 'The time when it is expired (timestamp in millisecond)' }), addType: t.arg.int({ required: true, defaultValue: 1, description: `Method: - 1-via phone bluetooth, should call APP SDK method to add passcode first. - 2-via gateway or WiFi lock, you can call this api directly if it\'s a WiFi lock or it\'s connected to gateway. The default value is 1.` }) }, resolve: (_, { smartlockId, passcodeType, passcodeName, passcode, startDate, endDate, addType }, context) => __awaiter(void 0, void 0, void 0, function* () { const { isAllowed } = context; if (!(yield isAllowed(context))) { throw new graphql_1.GraphQLError('Not allowed'); } const { clientId } = session_1.authHolder; const { accessToken } = session_1.authHolder.auth; if (!accessToken) { logger_1.logger.error(`Failed executing 'addPasscode': Err(10004): invalid token`); return { errcode: 10004, errmsg: 'invalid token' }; } const passcodeClient = new ttlock_api_1.PasscodeApi(); const response = yield passcodeClient.addPasscode({ clientId, accessToken, date: new Date().getTime(), lockId: +smartlockId, keyboardPwdType: passcodeType, keyboardPwdName: (0, utils_1.fixStringNullable)(passcodeName), keyboardPwd: passcode, addType: addType == 1 ? 1 : 2, startDate, endDate: (0, utils_1.fixNumberNullable)(endDate) }); if (response.errcode) { logger_1.logger.error(`Failed executing 'addPasscode': Err(${response.errcode}): ${response.errmsg}`); throw new graphql_1.GraphQLError(`Failed executing 'addPasscode': Err(${response.errcode}): ${response.errmsg}`); } return response; }) }), updatePasscode: t.field({ type: 'TTLockResponse', args: { smartlockId: t.arg.string({ required: true, description: 'Lock ID' }), passcodeId: t.arg.int({ required: true, description: 'Passcode Id' }), passcodeName: t.arg.string({ required: false, description: 'Passcode name' }), passcode: t.arg.int({ required: false, description: 'Passcode' }), startDate: t.arg.float({ required: false, description: 'The time when it becomes valid (timestamp in millisecond)' }), endDate: t.arg.float({ required: false, description: 'The time when it is expired (timestamp in millisecond)' }), changeType: t.arg.int({ required: true, defaultValue: 1, description: `Method: - 1-via phone bluetooth, should call APP SDK method to add passcode first. - 2-via gateway or WiFi lock, you can call this api directly if it\'s a WiFi lock or it\'s connected to gateway. The default value is 1.` }) }, resolve: (_, { smartlockId, passcodeId, passcodeName, passcode, startDate, endDate, changeType }, context) => __awaiter(void 0, void 0, void 0, function* () { const { isAllowed } = context; if (!(yield isAllowed(context))) { throw new graphql_1.GraphQLError('Not allowed'); } const { clientId } = session_1.authHolder; const { accessToken } = session_1.authHolder.auth; if (!accessToken) { logger_1.logger.error(`Failed executing 'updatePasscode': Err(10004): invalid token`); return { errcode: 10004, errmsg: 'invalid token' }; } const passcodeClient = new ttlock_api_1.PasscodeApi(); const response = yield passcodeClient.changePasscode({ clientId, accessToken, date: new Date().getTime(), lockId: +smartlockId, keyboardPwdId: passcodeId, keyboardPwdName: (0, utils_1.fixStringNullable)(passcodeName), newKeyboardPwd: (0, utils_1.fixNumberNullable)(passcode), changeType: changeType == 1 ? 1 : 2, startDate: (0, utils_1.fixNumberNullable)(startDate), endDate: (0, utils_1.fixNumberNullable)(endDate) }); if (response.errcode) { logger_1.logger.error(`Failed executing 'updatePasscode': Err(${response.errcode}): ${response.errmsg}`); throw new graphql_1.GraphQLError(`Failed executing 'updatePasscode': Err(${response.errcode}): ${response.errmsg}`); } return response; }) }), deletePasscode: t.field({ type: 'TTLockResponse', args: { smartlockId: t.arg.string({ required: true, description: 'Lock ID' }), passcodeId: t.arg.int({ required: true, description: 'Passcode Id' }), deleteType: t.arg.int({ required: true, defaultValue: 1, description: `Method: - 1-via phone bluetooth, should call APP SDK method to add passcode first. - 2-via gateway or WiFi lock, you can call this api directly if it\'s a WiFi lock or it\'s connected to gateway. The default value is 1.` }) }, resolve: (_, { smartlockId, passcodeId, deleteType }, context) => __awaiter(void 0, void 0, void 0, function* () { const { isAllowed } = context; if (!(yield isAllowed(context))) { throw new graphql_1.GraphQLError('Not allowed'); } const { clientId } = session_1.authHolder; const { accessToken } = session_1.authHolder.auth; if (!accessToken) { logger_1.logger.error(`Failed executing 'deletePasscode': Err(10004): invalid token`); return { errcode: 10004, errmsg: 'invalid token' }; } const passcodeClient = new ttlock_api_1.PasscodeApi(); const response = yield passcodeClient.deletePasscode({ clientId, accessToken, date: new Date().getTime(), lockId: +smartlockId, keyboardPwdId: passcodeId, deleteType: deleteType == 1 ? 1 : 2 }); if (response.errcode) { logger_1.logger.error(`Failed executing 'deletePasscode': Err(${response.errcode}): ${response.errmsg}`); throw new graphql_1.GraphQLError(`Failed executing 'deletePasscode': Err(${response.errcode}): ${response.errmsg}`); } return response; }) }), updateWirelessKeypad: t.field({ type: 'TTLockResponse', args: { wirelessKeypadId: t.arg.string({ required: true, description: 'Wireless keypad ID' }), wirelessKeypadName: t.arg.string({ required: true, description: 'New wireless keypad name' }) }, resolve: (_, { wirelessKeypadId, wirelessKeypadName }, context) => __awaiter(void 0, void 0, void 0, function* () { const { isAllowed } = context; if (!(yield isAllowed(context))) { throw new graphql_1.GraphQLError('Not allowed'); } const { clientId } = session_1.authHolder; const { accessToken } = session_1.authHolder.auth; if (!accessToken) { logger_1.logger.error(`Failed executing 'updateWirelessKeypad': Err(10004): invalid token`); return { errcode: 10004, errmsg: 'invalid token' }; } const client = new ttlock_api_1.WirelessKeypadApi(); const response = yield client.renameWirelessKeypad({ clientId, accessToken, date: new Date().getTime(), wirelessKeypadId: +wirelessKeypadId, wirelessKeypadName: wirelessKeypadName }); if (response.errcode) { logger_1.logger.error(`Failed executing 'updateWirelessKeypad': Err(${response.errcode}): ${response.errmsg}`); throw new graphql_1.GraphQLError(`Failed executing 'updateWirelessKeypad': Err(${response.errcode}): ${response.errmsg}`); } return response; }) }), deleteWirelessKeypad: t.field({ type: 'TTLockResponse', args: { wirelessKeypadId: t.arg.string({ required: true, description: 'Wireless keypad ID' }) }, resolve: (_, { wirelessKeypadId }, context) => __awaiter(void 0, void 0, void 0, function* () { const { isAllowed } = context; if (!(yield isAllowed(context))) { throw new graphql_1.GraphQLError('Not allowed'); } const { clientId } = session_1.authHolder; const { accessToken } = session_1.authHolder.auth; if (!accessToken) { logger_1.logger.error(`Failed executing 'deleteWirelessKeypad': Err(10004): invalid token`); return { errcode: 10004, errmsg: 'invalid token' }; } const client = new ttlock_api_1.WirelessKeypadApi(); const response = yield client.deleteWirelessKeypad({ clientId, accessToken, date: new Date().getTime(), wirelessKeypadId: +wirelessKeypadId }); if (response.errcode) { logger_1.logger.error(`Failed executing 'deleteWirelessKeypad': Err(${response.errcode}): ${response.errmsg}`); throw new graphql_1.GraphQLError(`Failed executing 'deleteWirelessKeypad': Err(${response.errcode}): ${response.errmsg}`); } return response; }) }) }) }); builder_1.builder.queryFields((t) => ({ ttlock: t.field({ type: 'TTLock', resolve: () => ({}) }) })); builder_1.builder.mutationFields((t) => ({ ttlock: t.field({ type: 'TTLockMutations', resolve: () => ({}) }) })); //# sourceMappingURL=ttlock.js.map