You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

608 lines
23 KiB

1 year ago
  1. CREATE PROCEDURE [dbo].[mobile_proc_userLogin]
  2. @flag VARCHAR(30)
  3. ,@userName VARCHAR(100) = NULL
  4. ,@Password VARCHAR(100) = NULL
  5. ,@Imei VARCHAR(256) = NULL --Imei Number/UUID or Android id of Mobile
  6. ,@accessCode VARCHAR(MAX) = NULL -- access token used in every request
  7. ,@scope VARCHAR(50) = NULL --for mobile applicatin, @scope='mobile_app'
  8. ,@clientId VARCHAR(200) = NULL --basic header key
  9. ,@fcmid VARCHAR(250) = NULL
  10. ,@customerId VARCHAR(100) = NULL
  11. ,@ip VARCHAR(100) = NULL
  12. ,@isBiometricLogin VARCHAR(5) = NULL
  13. ,@biometricLoginType VARCHAR(20) = NULL
  14. ,@deviceType VARCHAR(100) = NULL
  15. ,@accessToken VARCHAR(MAX) = NULL -- access token for Biometric
  16. AS
  17. BEGIN TRY
  18. ----------------------------------------
  19. --@fcmid varchar(250), add @IP param
  20. --#182 - @isBiometricLogin VARCHAR(5), @biometricLoginType VARCHAR(20) , modifications in @flag='l'
  21. -- @isbiometricEnabled
  22. -- #225 - flag = l, check accessCode and status for biometric login
  23. -- add new param @accessToken for Biometric
  24. --select column [mobile_userRegistration].isForcePassChange
  25. ----------------------------------------
  26. SET NOCOUNT ON;
  27. SET XACT_ABORT ON;
  28. DECLARE
  29. @UserData VARCHAR(500)
  30. ,@UserInfoDetail VARCHAR(MAX)
  31. ,@AccessCodeExpiresAfter INT = 5 --Minutes
  32. ,@email VARCHAR(100)
  33. ,@mobile VARCHAR(100)
  34. ,@customerPwd VARCHAR(50)
  35. ,@isActive CHAR(1)
  36. ,@isEmailVerified BIT
  37. ,@approvedDate DATETIME
  38. ,@isBiometricEnabled VARCHAR(10)
  39. ----------------------- Local variables declaration ###STARTS------------------------
  40. DECLARE
  41. @_imei VARCHAR(256)
  42. ,@_otpUsed BIT
  43. ,@_scope VARCHAR(50)
  44. ,@_isDeleted CHAR(1)
  45. ,@_errorMsg VARCHAR(MAX)
  46. ,@_isExists BIT=0
  47. ,@_Otp VARCHAR(50)
  48. ,@_accessCode VARCHAR(MAX)
  49. ,@_accessCodeExpiry DATETIME
  50. ,@_errorCode VARCHAR(20)
  51. ,@_lastLoggedInDevice VARCHAR(200)
  52. ,@previousAccessCode VARCHAR(MAX)
  53. ----------------------- Local variables declaration ###ENDS------------------------
  54. -- Check This Customer Infoagree State
  55. IF @flag = 'check-agree'
  56. BEGIN
  57. IF EXISTS(SELECT TOP 1 'X' FROM CustomerMasterTemp(NOLOCK) WHERE username = @userName AND ISNULL(agreeYn, '0') = '0' )
  58. BEGIN
  59. SELECT 0 ErrorCode,'Get PDF List Success About Agree infomation' Msg, NULL Id, rowId, PdfName, AgreePdfPath
  60. FROM customerAgreeDocumentTbl (NOLOCK)
  61. WHERE targetObj = 'STAGING'
  62. RETURN
  63. END
  64. ELSE IF EXISTS(SELECT TOP 1 'X' FROM CustomerMaster(NOLOCK) WHERE email = @userName AND ISNULL(agreeYn, '0') = '0' )
  65. BEGIN
  66. SELECT 0 ErrorCode,'Get PDF List Success About Agree infomation' Msg, NULL Id, rowId, PdfName, AgreePdfPath
  67. FROM customerAgreeDocumentTbl(NOLOCK)
  68. WHERE targetObj = 'STAGING'
  69. RETURN
  70. END
  71. ELSE
  72. BEGIN
  73. SELECT 1 ErrorCode,'Invaild user.. Check user value ' Msg, NULL Id
  74. END
  75. END
  76. ELSE IF @flag='l' -- login to system(done)
  77. BEGIN
  78. IF ISNULL(@username,'')=''
  79. BEGIN
  80. SELECT '1' ErrorCode, 'User Id not defined.' Msg ,NULL ID
  81. RETURN
  82. END
  83. SELECT @scope=dbo.mobile_FNAGetApplicationScope(@clientId)
  84. IF ISNULL(@scope,'')<>'mobile_app'
  85. BEGIN
  86. SELECT '2' ErrorCode, 'Application scope is not valid for this user.' Msg ,NULL ID
  87. RETURN
  88. END
  89. SET @UserData ='User: '+ @username + ' ,Password: ' + @Password + ' ,User Type:Mobile User' +' ,Device Info: '+@imei +
  90. ' ,IsBiometricLogin: ' + @isBiometricLogin + ' ,BiometricTypeLogin: '+@biometricLoginType + ' ,DeviceType: '+@deviceType
  91. + ' ,AccessCode: '+ ISNULL(@accessCode, '') + ' ,AccessToken: '+ ISNULL(@accessToken, '')
  92. DECLARE @isReferred BIT=0
  93. IF EXISTS(SELECT TOP 1 'x' FROM referralmaster(NOLOCK) WHERE email = @userName OR mobile = @userName)
  94. SET @isReferred=1
  95. SELECT
  96. @_lastLoggedInDevice=lastLoggedInDevice
  97. ,@_accessCodeExpiry=accessCodeExpiry
  98. ,@_accessCode=accessCode
  99. FROM dbo.mobile_userRegistration(NOLOCK)
  100. WHERE username = @userName
  101. --IF @_lastLoggedInDevice = @Imei AND @_accessCodeExpiry > GETDATE()
  102. --BEGIN
  103. -- SET @accessCode=@_accessCode
  104. -- UPDATE mobile_userRegistration SET
  105. -- accessCodeExpiry = DATEADD(MINUTE,@AccessCodeExpiresAfter,GETDATE()) --adding validity of access token for 20 minutes.
  106. -- FROM dbo.mobile_userRegistration(NOLOCK) ur
  107. -- WHERE ur.username = @userName --END
  108. --ELSE
  109. DECLARE @JmeclientId VARCHAR(50)
  110. SELECT @JmeclientId = DBO.DECRYPTDB(clientId) FROM KFTC_GME_MASTER (NOLOCK)
  111. BEGIN
  112. BEGIN TRANSACTION
  113. UPDATE mobile_userRegistration
  114. SET accessCode = @accessCode
  115. ,accessCodeExpiry = DATEADD(MINUTE,@AccessCodeExpiresAfter,GETDATE()) --adding validity of access token for 20 minutes.
  116. ,IMEI=@Imei
  117. ,lastLoggedInDevice = getdate()
  118. ,deviceId = @fcmid
  119. ,ipAddress=@ip
  120. ,BiometricLoginType = @biometricLoginType
  121. --,IsBiometricLogin = @isBiometricLogin
  122. ,DeviceType = @deviceType
  123. FROM dbo.mobile_userRegistration(nolock)
  124. WHERE username = @userName
  125. IF @@TRANCOUNT>0
  126. COMMIT TRANSACTION
  127. END
  128. DELETE T FROM customerMaster(NOLOCK)c
  129. INNER JOIN CustomerMasterTemp t ON c.username=t.username
  130. AND C.username = @userName
  131. DECLARE @redirectTo VARCHAR(100) = ''
  132. IF EXISTS(SELECT TOP 1 'X' FROM customerMasterTemp cm(NOLOCK) WHERE cm.username=@userName AND cm.customerPassword = dbo.FNAEncryptString(@Password))
  133. OR (EXISTS(SELECT TOP 1 'X' FROM customerMasterTemp cm(NOLOCK) INNER JOIN MobileToken mt(NOLOCK) ON cm.email = mt.userName
  134. WHERE cm.username=@userName AND @isBiometricLogin = 'TRUE' AND mt.token = @accessToken AND mt.activeStatus = 'Y'))
  135. BEGIN
  136. SELECT
  137. '0' ErrorCode
  138. ,@scope scope
  139. ,@userName userId
  140. ,cust.customerId SenderId
  141. ,ISNULL(cust.firstName,'') firstName
  142. -- ,ISNULL(cust.middleName,'') middleName
  143. -- ,ISNULL(cust.lastName1,'') lastName
  144. -- ,ISNULL(cust.nickName,'') nickName
  145. ,ISNULL(cust.email,'') email
  146. ,ISNULL(cust.customerEmail,'') customerEmail
  147. ,ISNULL(cust.mobile,'') phone
  148. -- ,cust.idType AS idType
  149. --,cust.idNumber AS idNumber
  150. ,cm.countryCode AS countryCode
  151. -- ,CAST(ISNULL(cust.bonusPoint,0) AS DECIMAL) rewardPoint
  152. ,CASE WHEN ISNULL(cust.isActive,'Y')='Y' THEN 1 ELSE 0 END active
  153. ,kyc = 0
  154. ,CASE WHEN ISNULL(cust.verifiedDate,'')<>'' THEN 1 ELSE 0 END verified
  155. ,ISNULL(cust.walletAccountNo,'') walletNumber
  156. ,0 availableBalance
  157. ,'Fast Remit Bank' primaryBankName
  158. -- ,'' primaryBankAccount
  159. ,@accessCode accessCode
  160. ,DATEDIFF(SS,GETDATE(),ur.accessCodeExpiry) expiresIn
  161. -- ,ISNULL(dpUrl,'') dpUrl
  162. -- ,ISNULL(ur.cmRegistrationId,'') cmRegistrationId
  163. ,'' country
  164. ,ISNULL(cust.city,'') city
  165. ,ISNULL(cust.[address],'') [address]
  166. -- ,ISNULL(cust.state2,'') [province]
  167. ,'' [provinceId]
  168. -- ,@isReferred isReferred
  169. ,'' sourceId
  170. ,yearlyLimit=0
  171. ,'0' AS pennyTestStatus
  172. ,gmeClientId = @JmeclientId
  173. ,redirectTo = @redirectTo
  174. ,customerType
  175. ,isExistingCustomer = ISNULL(isExistingCustomer, 1)
  176. ,IsBiometricLogin = ISNULL(IsBiometricLogin, 0)
  177. ,BiometricLoginType = ISNULL(BiometricLoginType, '')
  178. ,CASE ur.isForcePassChange WHEN '1' THEN 'Y' ELSE 'N' END IsForcedPwdChange
  179. ,ISNULL(@accessToken,'') accessToken
  180. FROM (SELECT TOP 1 * FROM customerMasterTemp(NOLOCK) cust WHERE cust.username = @userName)cust
  181. LEFT JOIN mobile_userRegistration(NOLOCK) ur ON cust.customerId=ur.customerId
  182. LEFT JOIN dbo.countryMaster(NOLOCK) AS CM ON cm.countryId=cust.nativeCountry
  183. --WHERE cust.username=@userName
  184. EXEC proc_applicationLogs @flag='login',@logType='Login Success', @createdBy = @username, @Reason='Login',@UserData = @UserData,@fieldValue = @UserInfoDetail
  185. RETURN
  186. END
  187. IF @isBiometricLogin = 'TRUE'
  188. BEGIN
  189. SELECT @isbiometricEnabled = (SELECT ISNULL(isBiometricLogin,0) FROM mobile_userRegistration where username = @userName)
  190. IF @isbiometricEnabled = 0
  191. BEGIN
  192. SELECT '3' ErrorCode, 'Biometric login is not enabled. Please login using password.' Msg ,NULL ID
  193. RETURN
  194. END
  195. IF ISNULL(@accessToken,'') = ''
  196. BEGIN
  197. SELECT '4' ErrorCode, 'Access Token is required for BioMetric login.' Msg ,NULL ID
  198. RETURN
  199. END
  200. IF NOT EXISTS (SELECT TOP 1 * FROM MobileToken(NOLOCK) WHERE userName = @userName AND token = @accessToken AND activeStatus = 'Y')
  201. BEGIN
  202. SELECT '5' ErrorCode, 'Unauthorized Access Token for BioMetric login.' Msg ,NULL
  203. RETURN
  204. END
  205. SELECT
  206. @email = email,
  207. @mobile = cm.mobile,
  208. @customerPwd = customerPassword,
  209. @isActive = isactive,
  210. @customerId = cm.customerId,
  211. @approvedDate = cm.approvedDate
  212. -- @customerId = cm.customerId
  213. FROM customerMaster (NOLOCK)cm
  214. WHERE cm.username = @userName
  215. AND cm.onlineUser='Y'
  216. END
  217. ELSE
  218. BEGIN
  219. SELECT
  220. @email = email,
  221. @mobile = cm.mobile,
  222. @customerPwd = customerPassword,
  223. @isActive = isactive,
  224. @customerId = cm.customerId,
  225. @approvedDate = cm.approvedDate
  226. -- @customerId = cm.customerId
  227. FROM customerMaster (NOLOCK)cm
  228. WHERE cm.username = @userName
  229. --OR cm.mobile=@userName ## NOT MOBILE LOGIN NOT APPLICABLE
  230. AND cm.customerPassword = dbo.FNAEncryptString(@Password)
  231. AND cm.onlineUser='Y'
  232. END
  233. IF @customerId IS NULL --OR @activeStatus = 'N'
  234. BEGIN
  235. SELECT '6' errorCode, 'The username/password do not match.' msg, @username id
  236. SET @UserInfoDetail = 'Reason = Incorrect username.'
  237. EXEC dbo.proc_applicationLogs
  238. @flag='login',
  239. @logType='Login fails',
  240. @createdBy = @username,
  241. @Reason='Invalid Username',
  242. @UserData = @UserData,
  243. @fieldValue = @UserInfoDetail
  244. RETURN
  245. END
  246. BEGIN TRANSACTION
  247. IF NOT EXISTS(SELECT TOP 1 'X' FROM mobile_userRegistration ur(NOLOCK) WHERE ur.username = @userName)
  248. BEGIN
  249. INSERT INTO mobile_userRegistration (customerId,username, OTP,OTP_Used,createdDate,IMEI,clientId,deviceId,ipAddress,BiometricLoginType, IsBiometricLogin, DeviceType)
  250. SELECT @customerId,@userName,0,0,GETDATE(),@Imei,@clientId,@fcmid,@ip, @biometricLoginType, @isBiometricLogin, @deviceType
  251. UPDATE cm SET cm.isEmailVerified=1
  252. FROM dbo.customerMaster cm WHERE cm.customerId=@customerId
  253. END
  254. IF @@TRANCOUNT>0
  255. COMMIT TRANSACTION
  256. DECLARE @attemptsCount INT
  257. SELECT TOP 1 @attemptsCount = loginAttemptCount FROM passwordFormat WITH(NOLOCK)
  258. IF (ISNULL(@isActive, 'Y') = 'N')
  259. BEGIN
  260. SELECT '7' errorCode, 'Your account is Inactive. Please, contact GME Support Team.' msg, @userName id
  261. SET @UserInfoDetail = 'Reason = Login fails, Your account is Inactive. Please, contact your administrator.'
  262. EXEC proc_applicationLogs
  263. @flag='login',
  264. @logType='Login fails',
  265. @createdBy = @userName,
  266. @Reason='User is not active ',
  267. @UserData = @UserData,
  268. @fieldValue = @UserInfoDetail
  269. RETURN
  270. END
  271. UPDATE customerMaster
  272. SET lastLoginTs = GETDATE()
  273. WHERE customerId=@customerId
  274. DECLARE @yearlyLimit VARCHAR(100)=''
  275. DECLARE @totalSend MONEY, @totalSendText VARCHAR(200), @YearStart DATE, @YearEnd DATETIME
  276. SELECT @YearStart = DATEADD(yy, DATEDIFF( yy, 0, GETDATE()), 0)
  277. ,@YearEnd = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1)+' 23:59:59'
  278. SELECT @totalSend = SUM(R.cAmt)
  279. FROM REMITTRAN R(NOLOCK)
  280. INNER JOIN TRANSENDERS T(NOLOCK) ON T.TRANID = R.ID
  281. AND T.CUSTOMERID = @customerId
  282. AND R.TRANSTATUS <> 'Cancel'
  283. AND R.approvedDate BETWEEN @YearStart AND @YearEnd
  284. SELECT @yearlyLimit = amount
  285. FROM dbo.csDetail CD(NOLOCK)
  286. INNER JOIN csMaster CM(NOLOCK) ON CM.csMasterId = CD.csMasterId
  287. AND CD.period = 365
  288. AND CD.condition = 4600
  289. AND ISNULL(CD.isActive, 'Y') = 'Y'
  290. AND ISNULL(CD.isDeleted, 'N') = 'N'
  291. AND ISNULL(CD.isEnable, 'Y') = 'Y'
  292. AND ISNULL(CM.isActive, 'Y') = 'Y'
  293. AND ISNULL(CM.isDeleted, 'N') = 'N'
  294. SET @yearlyLimit = (@yearlyLimit - ISNULL(@totalSend, 0))
  295. DECLARE @hasPennyTestDone VARCHAR(1)='1'
  296. IF EXISTS(SELECT TOP 1 'x' FROM dbo.customerMaster(NOLOCK) WHERE approvedDate < '2018-12-19' AND customerId = @customerId)
  297. BEGIN
  298. SET @hasPennyTestDone = '2'
  299. END
  300. SELECT
  301. '0' ErrorCode
  302. ,@scope scope
  303. ,@userName userId
  304. ,cust.customerId SenderId
  305. ,ISNULL(cust.firstName,'') firstName
  306. -- ,ISNULL(cust.middleName,'') middleName
  307. -- ,ISNULL(cust.lastName1,'') lastName
  308. -- ,'' nickName
  309. ,ISNULL(cust.email,'') email
  310. ,ISNULL(cust.customerEmail,'') customerEmail
  311. ,ISNULL(cust.mobile,'') phone
  312. -- ,cust.idType AS idType
  313. --,cust.idNumber AS idNumber
  314. ,cm1.countryCode AS countryCode
  315. ,CAST(ISNULL(cust.bonusPoint,0) AS DECIMAL) rewardPoint
  316. ,CASE WHEN ISNULL(cust.isActive,'Y')='Y' THEN 1 ELSE 0 END active
  317. ,ISNULL(HasDeclare, 0) kyc
  318. ,CASE WHEN ISNULL(isExistingCustomer, 1) = 0 THEN CASE WHEN cust.ApprovedDate IS NOT NULL THEN 1 ELSE 0 END
  319. ELSE CASE WHEN mobileVerifiedDate IS NOT NULL THEN 1 ELSE 0 END END verified
  320. ,ISNULL(cust.walletAccountNo,'') walletNumber
  321. ,CAST([dbo].FNAGetCustomerACBal(@email) AS DECIMAL) availableBalance
  322. --,ISNULL(bl.BankName,'') primaryBankName
  323. ,primaryBankName='Fast Remit Bank'--CASE WHEN cust.customerType='11048' THEN 'Mutual savings bank' ELSE 'Kwangju Bank (034)' END
  324. -- ,'' AS primaryBankAccount
  325. ,@accessCode accessCode
  326. ,DATEDIFF(SS,GETDATE(),ur.accessCodeExpiry) expiresIn
  327. -- ,'' dpUrl
  328. -- ,ISNULL(ur.cmRegistrationId,'') cmRegistrationId
  329. ,ISNULL(co.countryName,'') country
  330. ,ISNULL(cust.city,'') city
  331. ,ISNULL(cust.[address],'') [address]
  332. -- ,ISNULL(cust.state2,'') [province]
  333. ,ISNULL(cm.cityId,'') [provinceId]
  334. -- ,@isReferred isReferred
  335. ,ISNULL(sdv.valueId,'') sourceId
  336. ,yearlyLimit=@yearlyLimit
  337. ,PennyTestStatus=@hasPennyTestDone-----0 not started, 1 requested , 2 completed
  338. , gmeClientId = @JmeclientId
  339. ,redirectTo = @redirectTo
  340. ,customerType
  341. ,isExistingCustomer = ISNULL(isExistingCustomer, 1)
  342. ,IsBiometricLogin = ISNULL(IsBiometricLogin, 0)
  343. ,BiometricLoginType = ISNULL(BiometricLoginType, '')
  344. ,CASE ur.isForcePassChange WHEN '1' THEN 'Y' ELSE 'N' END IsForcedPwdChange
  345. ,ISNULL(@accessToken,'') accessToken
  346. FROM (SELECT TOP 1 * FROM customerMaster(NOLOCK) cust WHERE cust.customerId=@customerId)cust
  347. LEFT JOIN dbo.countryMaster AS CM1 ON cm1.countryId=cust.nativeCountry
  348. LEFT JOIN mobile_userRegistration(NOLOCK) ur ON cust.customerId=ur.customerId
  349. LEFT JOIN dbo.vwBankLists (NOLOCK) bl ON cust.bankName=bl.bankCode
  350. LEFT JOIN countryMaster co(NOLOCK) ON cust.country=co.countryId
  351. LEFT JOIN staticDatavalue sdv(NOLOCK) ON cust.sourceOfFund=sdv.detailTitle AND ISNULL(sdv.IS_DELETE,'N')='N'
  352. LEFT JOIN dbo.CityMaster cm(NOLOCK) ON cust.state2 = cm.cityName
  353. --WHERE cust.customerId=@customerId AND ISNULL(sdv.IS_DELETE,'N')='N'
  354. EXEC proc_applicationLogs
  355. @flag='login',
  356. @logType='Login Success',
  357. @createdBy = @username,
  358. @Reason='Login',
  359. @UserData = @UserData,
  360. @fieldValue = @UserInfoDetail
  361. RETURN
  362. END
  363. ELSE IF @flag='chk-access-code' --validating access code(done)
  364. BEGIN
  365. --SELECT
  366. -- @_accessCodeExpiry=l.accessCodeExpiry
  367. -- ,@username=ISNULL(l.username,cust.email)
  368. -- ,@_scope=ISNULL(a.scope,'')
  369. -- ,@_lastLoggedInDevice=ISNULL(cust.lastLoggedInDevice,'')
  370. -- ,@_imei =ISNULL(cust.lastLoggedInDevice,'')
  371. --FROM customermasterTemp(NOLOCK) cust
  372. --INNER JOIN customermaster(NOLOCK) main ON main.customerId=cust.customerMasterId
  373. --LEFT JOIN mobile_userRegistration(NOLOCK) l ON cust.customerId=l.customerId
  374. --INNER JOIN mobile_GmeApiClientRegistration a(NOLOCK) ON a.clientId=l.clientId
  375. --WHERE l.accessCode=@accessCode
  376. SELECT
  377. @_accessCodeExpiry=MUR.accessCodeExpiry
  378. ,@username= mur.username
  379. ,@_scope=ISNULL(a.scope,'')
  380. ,@_lastLoggedInDevice=mur.lastLoggedInDevice
  381. ,@_imei = mur.IMEI
  382. ,@customerId = mur.rowId
  383. FROM dbo.mobile_userRegistration AS MUR(NOLOCK)
  384. INNER JOIN mobile_GmeApiClientRegistration a(NOLOCK) ON a.clientId=MUR.clientId
  385. AND MUR.accessCode = @accessCode
  386. AND (mur.IMEI = @Imei OR mur.lastLoggedInDevice=@Imei)
  387. --PRINT @_accessCodeExpiry
  388. --IF NOT EXISTS(SELECT 'x' FROM dbo.mobile_userRegistration AS MUR WHERE MUR.accessCode=@accessCode AND MUR.IMEI=@Imei)
  389. --BEGIN
  390. -- SELECT '2' errorCode, 'Access code expired..' Msg ,NULL ID
  391. -- RETURN
  392. --END
  393. --IF NOT EXISTS(SELECT 'x' FROM dbo.mobile_userRegistration AS MUR WHERE MUR.accessCode=@accessCode AND MUR.lastLoggedInDevice=@Imei)
  394. --BEGIN
  395. -- SELECT '2' errorCode, 'Access code expired..' Msg ,NULL ID
  396. -- RETURN
  397. --END
  398. IF ISNULL(@username,'')='' AND ISNULL(@_lastLoggedInDevice,'') = @imei
  399. BEGIN
  400. SELECT '8' errorCode, 'Access code expired..' Msg ,NULL ID
  401. RETURN
  402. END
  403. IF ISNULL(@username,'')='' AND ISNULL(@_lastLoggedInDevice,'') <> @imei
  404. BEGIN
  405. SELECT '9' errorCode, 'Access code expired..' Msg ,NULL ID
  406. RETURN
  407. END
  408. IF (@_accessCodeExpiry < GETDATE())
  409. BEGIN
  410. SELECT '10' errorCode, 'It seems like you are using old access code. Please use newly generated access code.' Msg ,NULL ID
  411. RETURN
  412. END
  413. ------------- ### Check if the user trying to validate access-code exists or not ###STARTS------------
  414. IF @username IS NULL
  415. BEGIN
  416. SELECT @_errorMsg = 'User with contact Info ' + @username + ' doesnot exists. If you are a new user, then sign up and proceed further.'
  417. SELECT '1' ErrorCode, @_errorMsg Msg ,NULL ID
  418. RETURN
  419. END
  420. ------------- ### Check if the user trying to validate access-code exists or not ###ENDS------------
  421. IF @_lastLoggedInDevice <> @Imei
  422. BEGIN
  423. SELECT '11' errorCode, 'You are logged in from another device.' Msg ,NULL ID
  424. RETURN
  425. END
  426. ELSE IF ISNULL(@_scope,'')<>'mobile_app'
  427. BEGIN
  428. SELECT '12' ErrorCode, 'Application scope is not valid for this user.' Msg ,NULL ID
  429. RETURN
  430. END
  431. UPDATE mobile_userRegistration
  432. SET accessCodeExpiry = DATEADD(MINUTE,@AccessCodeExpiresAfter,GETDATE()) --expiry time for access code(token) increased to 20 minutes.
  433. WHERE rowId = @customerId
  434. SELECT '0' ErrorCode, 'Success' Msg ,@_scope ID
  435. RETURN
  436. END
  437. ELSE IF @flag='s-accesscode' --validating accessCode for "Get user by access code"
  438. BEGIN
  439. IF @scope<>'social_comp'
  440. BEGIN
  441. SELECT '13' ErrorCode, 'Application scope is not valid for this user.' Msg ,NULL ID
  442. RETURN
  443. END
  444. DECLARE @user VARCHAR(100);
  445. SELECT
  446. @email=cm.email,
  447. @mobile=cm.mobile,
  448. @isEmailVerified=cm.isEmailVerified,
  449. @customerId=cm.customerId
  450. FROM dbo.customerMaster(NOLOCK) cm
  451. INNER JOIN dbo.mobile_userRegistration(NOLOCK) ur
  452. ON cm.customerId=ur.customerId
  453. AND ur.accessCode=@accessCode
  454. IF @email IS NOT NULL AND @isEmailVerified=1
  455. BEGIN
  456. SET @user=@email
  457. END
  458. ELSE
  459. BEGIN
  460. SET @user=@mobile
  461. END
  462. SELECT
  463. @_imei=l.imei
  464. ,@_accessCodeExpiry=l.accessCodeExpiry
  465. ,@username= @user
  466. FROM customermaster cust (NOLOCK)
  467. INNER JOIN mobile_userRegistration l(NOLOCK) ON cust.customerId=l.customerId
  468. AND l.accessCode=@accessCode
  469. IF DATEDIFF(MINUTE, GETDATE() ,@_accessCodeExpiry)>0
  470. BEGIN
  471. SELECT
  472. '0' ErrorCode
  473. ,@userName userId
  474. ,ISNULL(cust.firstName,'') firstName
  475. ,ISNULL(cust.middleName,'') middleName
  476. ,ISNULL(cust.lastName1,'') lastName
  477. ,ISNULL(cm.countryName,'') nativeCountry
  478. ,'' nickName
  479. ,userRoles=''
  480. ,'' dpUrl
  481. ,ISNULL(ur.cmRegistrationId,'') cmRegistrationId
  482. ,ISNULL(ur.IMEI,'') uuid
  483. FROM (SELECT TOP 1 * FROM customerMaster(NOLOCK) cust WHERE cust.customerId=@customerId)cust
  484. LEFT JOIN mobile_userRegistration ur(NOLOCK) ON cust.customerId=ur.customerId
  485. LEFT JOIN dbo.countryMaster cm(NOLOCK) ON cust.nativeCountry=cm.countryId
  486. --WHERE cust.customerId=@customerId
  487. --WHERE cust.email=@userName OR cust.mobile=@userName
  488. RETURN
  489. END
  490. ELSE IF DATEDIFF(MINUTE, GETDATE() ,@_accessCodeExpiry)<=0
  491. BEGIN
  492. SELECT '14' ErrorCode, 'Access code expired.' Msg ,NULL ID
  493. RETURN
  494. END
  495. ELSE
  496. BEGIN
  497. SELECT '15' ErrorCode, 'Access code does not match.' Msg ,NULL ID
  498. RETURN
  499. END
  500. END
  501. ELSE IF @flag='ckeck-guid'
  502. BEGIN
  503. IF EXISTS(SELECT TOP 1 'A' FROM dbo.mobile_userRegistration(NOLOCK) WHERE accessCode IS NULL AND username = @userName )
  504. BEGIN
  505. UPDATE dbo.mobile_userRegistration SET accessCode = @accessCode WHERE accessCode IS NULL AND username = @userName
  506. END
  507. IF EXISTS(SELECT TOP 1 'A' FROM dbo.mobile_userRegistration(NOLOCK) WHERE accessCode = @accessCode AND username = @userName )
  508. BEGIN
  509. SELECT '16' ErrorCode, 'The access code is valid and is from trusted customer.' Msg, NULL Id
  510. RETURN
  511. END
  512. BEGIN
  513. SELECT '17' ErrorCode, 'No such access code found in system. Invalid username and customer is not trusted.' Msg, NULL Id
  514. RETURN
  515. END
  516. END
  517. ELSE IF @flag='get-device'
  518. BEGIN
  519. SELECT deviceId FROM mobile_userRegistration(NOLOCK) WHERE customerId = @customerId
  520. RETURN
  521. END
  522. ELSE IF @flag='agentDetail'
  523. BEGIN
  524. SELECT agentId,
  525. agentName,
  526. agentState,
  527. agentCity,
  528. agentAddress,
  529. agentZip,
  530. agentPhone1
  531. FROM AGENTMASTER (NOLOCK)
  532. WHERE PARENTID = '393877'
  533. -- WHERE PARENTID = 394399
  534. AND ACTASBRANCH = 'Y'
  535. RETURN
  536. END
  537. END TRY
  538. BEGIN CATCH
  539. IF @@TRANCOUNT > 0
  540. ROLLBACK TRANSACTION
  541. DECLARE @errorMessage VARCHAR(MAX)
  542. SET @errorMessage = ERROR_MESSAGE()
  543. SELECT '1' ErrorCode, @errorMessage Msg ,NULL ID
  544. END CATCH