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.

942 lines
29 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[mobile_proc_userRegistration] Script Date: 7/4/2019 11:35:48 AM ******/
  4. DROP PROCEDURE [dbo].[mobile_proc_userRegistration]
  5. GO
  6. /****** Object: StoredProcedure [dbo].[mobile_proc_userRegistration] Script Date: 7/4/2019 11:35:48 AM ******/
  7. SET ANSI_NULLS ON
  8. GO
  9. SET QUOTED_IDENTIFIER ON
  10. GO
  11. ALTER PROCEDURE mobile_proc_userRegistration
  12. @flag VARCHAR(30)
  13. ,@MSISDN VARCHAR(20) = NULL --Mobile Number
  14. ,@email VARCHAR(100) = NULL
  15. ,@code VARCHAR(50) = NULL --one time password
  16. ,@codeType VARCHAR(30) = NULL --one time password
  17. ,@userName VARCHAR(100) = NULL
  18. ,@Password VARCHAR(100) = NULL
  19. ,@newPassword VARCHAR(100) = NULL
  20. ,@Imei VARCHAR(256) = NULL --Imei Number of Mobile
  21. ,@appVersion VARCHAR(100) = NULL
  22. ,@deviceId VARCHAR(100) = NULL
  23. ,@osVersion VARCHAR(100) = NULL
  24. ,@phoneBrand VARCHAR(100) = NULL
  25. ,@phoneOs VARCHAR(100) = NULL
  26. ,@scope VARCHAR(50) = NULL
  27. ,@clientId VARCHAR(100) = NULL
  28. ,@answer DATE = NULL --password secutiry question applies for the dob
  29. ,@refWalletAccNo VARCHAR(50) = NULL
  30. ,@cmRegistrationId VARCHAR(MAX) = NULL
  31. ,@dpUrl VARCHAR(300) = NULL
  32. AS
  33. SET NOCOUNT ON;
  34. SET XACT_ABORT ON;
  35. BEGIN TRY
  36. IF ISNULL(@username,'')=''
  37. BEGIN
  38. SELECT '1' ErrorCode, 'User Id not defined.' Msg ,NULL ID
  39. RETURN
  40. END
  41. IF ISNULL(@scope,'')<>'mobile_app'
  42. BEGIN
  43. SELECT '1' ErrorCode, 'Application scope is not valid for this user.' Msg ,NULL ID
  44. RETURN
  45. END
  46. ----------------------- Local variables declaration ###STARTS------------------------
  47. DECLARE @_customerId BIGINT
  48. ,@_imei VARCHAR(256)
  49. ,@_otpUsed BIT
  50. ,@_scope VARCHAR(50)
  51. ,@_isDeleted CHAR(1)
  52. ,@_errorMsg VARCHAR(MAX)
  53. ,@_isExists BIT=0
  54. ,@_Otp VARCHAR(50)
  55. ,@_pwdRecoveryCode VARCHAR(50)
  56. ,@_dob DATE
  57. ,@_errorCode VARCHAR(50)
  58. ,@_isVerified BIT=0
  59. ,@_password VARCHAR(50)
  60. ,@_createdDate DATETIME
  61. ,@_count INT
  62. ,@_onlineUser CHAR(1)
  63. ,@_createdBy VARCHAR(100)
  64. ----------------------- Local variables declaration ###ENDS------------------------
  65. IF @flag='signup' --first time user create(DONE)
  66. BEGIN
  67. ------------- ### Check if the user trying to signup is already exists ###STARTS------------
  68. IF EXISTS(
  69. SELECT 'x' FROM dbo.customerMaster(NOLOCK) cm
  70. WHERE cm.email=@userName OR cm.mobile=@userName
  71. )
  72. BEGIN
  73. SELECT '1' ErrorCode , @userName +' already exists' Msg ,@userName ID
  74. RETURN;
  75. END
  76. IF EXISTS(
  77. SELECT 'x' FROM dbo.customerMasterTemp(NOLOCK) cm
  78. WHERE cm.email=@userName OR cm.mobile=@userName
  79. )
  80. BEGIN
  81. SELECT '1' ErrorCode , @userName +' already exists' Msg ,@userName ID
  82. RETURN;
  83. END
  84. ------------- ### Check if the user trying to signup is already exists ###ENDS------------
  85. ------------- ### Check device registration limit ###STARTS------------
  86. --IF EXISTS(SELECT 'x' FROM mobile_userRegistration(NOLOCK) WHERE imei=@imei GROUP BY imei HAVING COUNT(imei)>5)
  87. --BEGIN
  88. -- SELECT @_errorMsg = 'This device has exceeded the app registration limit.'
  89. -- SELECT '1' ErrorCode, @_errorMsg Msg ,NULL ID
  90. -- RETURN
  91. --END
  92. ------------- ### Check device registration limit ###ENDS------------
  93. BEGIN TRANSACTION
  94. INSERT INTO customerMasterTemp(mobile,email,customerpassword,createdBy)
  95. SELECT @MSISDN,@email,dbo.FNAEncryptString(@Password),@scope
  96. SET @_customerId=SCOPE_IDENTITY()
  97. INSERT INTO mobile_userRegistration (customerId,username, OTP,OTP_Used,createdDate,IMEI,clientId)
  98. SELECT @_customerId,@userName,@code,0,GETDATE(),@Imei,@clientId
  99. INSERT INTO OTPHistory(username,OTP,OTP_Used,createdDate,codeType,customerId)
  100. SELECT @userName,@code,0,GETDATE(),'dvc',@_customerId
  101. IF @@TRANCOUNT > 0
  102. COMMIT TRANSACTION
  103. SELECT '0' ErrorCode , 'Registration Success' Msg ,@userName ID
  104. RETURN
  105. -------## if record already exists ##ENDS---------------
  106. END
  107. ELSE IF @flag='v-otp' --validate otp (DONE)
  108. BEGIN
  109. --SELECT '1' ErrorCode, 'Invalid device verification code.' Msg ,NULL ID
  110. --RETURN
  111. IF ISNULL(@code,'')=''
  112. BEGIN
  113. SELECT '1' ErrorCode, 'Device verification code is required.' Msg ,NULL ID
  114. RETURN
  115. END
  116. IF ISNULL(@phoneOs,'') NOT IN('android','ios')
  117. BEGIN
  118. SELECT '1' ErrorCode, 'Invalid Device type(PhoneOs).' Msg ,NULL ID
  119. RETURN
  120. END
  121. ------------- ### Check if the user trying to validate is already exists ###STARTS------------
  122. IF NOT EXISTS(
  123. SELECT 'x' FROM dbo.customerMasterTemp(NOLOCK) cm
  124. WHERE (cm.email=@userName OR cm.mobile=@userName) AND ISNULL(cm.isDeleted,'N')='N'
  125. )
  126. BEGIN
  127. SELECT @_errorMsg = 'User with contact Info ' + @username + ' does not exists. If you are a new user, then sign up and proceed further.'
  128. SELECT '1' ErrorCode, @_errorMsg Msg ,NULL ID
  129. RETURN
  130. END
  131. ------------- ### Check if the user trying to validate is already exists ###ENDS------------
  132. SELECT
  133. @_customerId=cm.customerId
  134. FROM dbo.customerMasterTemp(NOLOCK) cm
  135. WHERE cm.email=@userName OR cm.mobile=@userName
  136. SELECT TOP 1
  137. @_imei=v.imei
  138. ,@_Otp=otp.OTP
  139. ,@_otpUsed=otp.OTP_Used
  140. ,@_isDeleted=ISNULL(cust.isDeleted,'N')
  141. ,@_createdDate=DATEADD(MINUTE,20,otp.createdDate)
  142. FROM customermasterTemp(NOLOCK) cust
  143. LEFT JOIN mobile_userRegistration(NOLOCK) v ON cust.customerId=v.customerId
  144. INNER JOIN dbo.OTPHistory(NOLOCK) otp ON cust.customerId=otp.customerId
  145. WHERE cust.customerId=@_customerId AND otp.codeType='dvc'
  146. ORDER BY otp.rowId DESC
  147. --INNER JOIN OTPHistory otp(NOLOCK) ON v.username=otp.username
  148. --WHERE cust.customerId=@_customerId AND otp.codeType='dvc'
  149. --ORDER BY otp.rowId DESC
  150. --SELECT TOP 1
  151. -- @_imei=v.imei
  152. -- ,@_Otp=otp.OTP
  153. -- ,@_otpUsed=otp.OTP_Used
  154. -- ,@_isDeleted=ISNULL(cust.isDeleted,'N')
  155. -- ,@_createdDate=DATEADD(MINUTE,20,otp.createdDate)
  156. --FROM customermaster(NOLOCK) cust
  157. --LEFT JOIN mobile_userRegistration(NOLOCK) v ON cust.customerId=v.customerId
  158. --INNER JOIN OTPHistory otp(NOLOCK) ON v.username=otp.username
  159. --WHERE cust.customerId=@_customerId AND otp.codeType='dvc'
  160. --ORDER BY otp.rowId DESC
  161. IF @_Otp=@code AND DATEDIFF(MINUTE, GETDATE() ,@_createdDate)>0
  162. AND @_imei=@Imei AND @_otpUsed=0
  163. BEGIN
  164. BEGIN TRANSACTION
  165. UPDATE mobile_userRegistration
  166. SET OTP_Used = 1
  167. ,appVersion=@appVersion
  168. ,phoneBrand=@phoneBrand
  169. ,phoneOs=@phoneOs
  170. ,osVersion=@osVersion
  171. ,deviceId=@deviceId
  172. WHERE imei=@Imei AND otp=@code
  173. --UPDATE OTPHistory
  174. -- SET OTP_Used = 1
  175. --WHERE username=@userName AND otp=@code
  176. UPDATE OTPHistory
  177. SET OTP_Used = 1
  178. WHERE customerId=@_customerId AND otp=@code
  179. IF EXISTS(SELECT 'x' FROM dbo.customerMasterTemp(NOLOCK)cm WHERE cm.email=@userName)
  180. BEGIN
  181. UPDATE cm
  182. SET cm.isEmailVerified=1,
  183. cm.isMobileVerified=0
  184. FROM dbo.customerMasterTemp(NOLOCK) cm WHERE cm.email=@userName
  185. END
  186. ELSE
  187. BEGIN
  188. UPDATE cm
  189. SET cm.isMobileVerified=1,
  190. cm.isEmailVerified=0
  191. FROM dbo.customerMasterTemp(NOLOCK) cm WHERE cm.mobile=@userName
  192. END
  193. COMMIT TRANSACTION
  194. SELECT '0' ErrorCode, 'Success' Msg, NULL ID
  195. RETURN
  196. END
  197. ELSE IF ISNULL(@_Otp,'')<>@code
  198. BEGIN
  199. SELECT '1' ErrorCode, 'Please enter valid verification code sent to your ' + @userName Msg ,NULL ID
  200. RETURN
  201. END
  202. ELSE IF DATEDIFF(MINUTE, GETDATE() ,@_createdDate)<=0
  203. BEGIN
  204. SELECT '1' ErrorCode, 'Device verification code expired. Please re-send code and try again.' Msg ,NULL ID
  205. RETURN
  206. END
  207. ELSE IF @_imei=@Imei AND @_otpUsed=1
  208. BEGIN
  209. SELECT '1' ErrorCode, 'User already verified.' Msg ,NULL ID
  210. RETURN
  211. END
  212. ELSE IF @_imei<>@Imei
  213. BEGIN
  214. SELECT '1' ErrorCode, 'User does not belongs to this device for verification.' Msg ,NULL ID
  215. RETURN
  216. END
  217. ELSE
  218. BEGIN
  219. SELECT '1' ErrorCode, 'Invalid device verification code.' Msg ,NULL ID
  220. RETURN
  221. END
  222. END
  223. ELSE IF @flag='re-code' --resend OTP(DONE)
  224. BEGIN
  225. IF ISNULL(@scope,'')<>'mobile_app'
  226. BEGIN
  227. SELECT '1' ErrorCode, 'Application scope is not valid for this user.' Msg ,NULL ID
  228. RETURN
  229. END
  230. IF ISNULL(@codeType,'') NOT IN('dvc','prc')
  231. BEGIN
  232. SELECT '1' ErrorCode, 'Requested code type is not valid.' Msg ,NULL ID
  233. RETURN
  234. END
  235. DECLARE @today VARCHAR(15)=CONVERT(DATE, GETDATE(),101)
  236. ------------- ### Check if the user trying to re-send code is exists or not ###STARTS------------
  237. IF EXISTS(
  238. SELECT 'x' FROM dbo.CustomerMasterTemp(NOLOCK) cm
  239. WHERE (cm.email=@userName OR cm.mobile=@userName) AND ISNULL(cm.isDeleted,'N')='N'
  240. )
  241. BEGIN
  242. SELECT
  243. @_customerId=cm.customerId
  244. FROM dbo.customerMasterTemp(NOLOCK) cm
  245. WHERE cm.email=@userName OR cm.mobile=@userName
  246. SELECT TOP 1
  247. @_imei=v.imei
  248. ,@_Otp=otp.OTP
  249. ,@_otpUsed=otp.OTP_Used
  250. FROM dbo.CustomerMasterTemp(NOLOCK) cust
  251. LEFT JOIN mobile_userRegistration(NOLOCK) v ON cust.customerId=v.customerId
  252. INNER JOIN OTPHistory otp(NOLOCK) ON cust.customerId=otp.customerId
  253. WHERE cust.customerId=@_customerId AND otp.codeType=@codeType
  254. ORDER BY otp.rowId DESC
  255. IF @_otpUsed=1 AND @codeType='dvc' --for device verification code
  256. BEGIN
  257. SELECT '1' ErrorCode, 'User already verified. You cannot re-send code for the user who is already verified.' Msg ,NULL ID
  258. RETURN
  259. END
  260. SELECT @_count=COUNT('x')
  261. FROM dbo.CustomerMasterTemp(NOLOCK) cust
  262. LEFT JOIN mobile_userRegistration(NOLOCK) v ON cust.customerId=v.customerId
  263. INNER JOIN OTPHistory otp(NOLOCK) ON cust.customerId=otp.customerId
  264. WHERE cust.customerId=@_customerId
  265. AND otp.codeType=@codeType
  266. AND v.imei=@imei
  267. AND otp.createdDate BETWEEN @today AND @today+ ' 23:59:59'
  268. IF ISNULL(@_count,0)>10
  269. BEGIN
  270. SELECT '1' ErrorCode, 'Your device has been blocked. Re-send code attempts exceeded from same device.' Msg ,NULL ID
  271. RETURN
  272. END
  273. INSERT INTO OTPHistory(username,OTP,OTP_Used,createdDate,codeType,customerId)
  274. SELECT @userName,@code,0,GETDATE(),@codeType,@_customerId
  275. SELECT '0' ErrorCode, 'Success' Msg ,@code ID
  276. RETURN
  277. END
  278. IF NOT EXISTS(
  279. SELECT 'x' FROM dbo.customerMaster(NOLOCK) cm
  280. WHERE (cm.email=@userName OR cm.mobile=@userName) AND ISNULL(cm.isDeleted,'N')='N'
  281. )
  282. BEGIN
  283. SELECT @_errorMsg = 'Invalid UserId.'
  284. SELECT '1' ErrorCode, @_errorMsg Msg ,NULL ID
  285. RETURN
  286. END
  287. ------------- ### Check if the user trying to re-send code is exists or not ###ENDS------------
  288. SELECT
  289. @_customerId=cm.customerId
  290. FROM dbo.customerMaster(NOLOCK) cm
  291. WHERE cm.email=@userName OR cm.mobile=@userName
  292. --SELECT TOP 1
  293. -- @_imei=v.imei
  294. -- ,@_Otp=otp.OTP
  295. -- ,@_otpUsed=otp.OTP_Used
  296. --FROM mobile_userRegistration(NOLOCK) v
  297. --LEFT JOIN OTPHistory otp(NOLOCK) ON v.username=otp.username
  298. --WHERE otp.username=@userName AND otp.codeType=@codeType
  299. --ORDER BY otp.rowId DESC
  300. SELECT TOP 1
  301. @_imei=v.imei
  302. ,@_Otp=otp.OTP
  303. ,@_otpUsed=otp.OTP_Used
  304. FROM customermaster(NOLOCK) cust
  305. LEFT JOIN mobile_userRegistration(NOLOCK) v ON cust.customerId=v.customerId
  306. INNER JOIN OTPHistory otp(NOLOCK) ON cust.customerId=otp.customerId
  307. WHERE cust.customerId=@_customerId AND otp.codeType=@codeType
  308. ORDER BY otp.rowId DESC
  309. ------------- ### Check if the user trying to re-send code is already verified or not ###STARTS------------
  310. IF @_otpUsed=1 AND @codeType='dvc' --for device verification code
  311. BEGIN
  312. SELECT '1' ErrorCode, 'User already verified. You cannot re-send code for the user who is already verified.' Msg ,NULL ID
  313. RETURN
  314. END
  315. ------------- ### Check if the user trying to re-send code is already verified or not ###ENDS------------
  316. ------------- ### Check attempts of re-send code from same device ###STARTS------------
  317. SELECT @_count=COUNT('x')
  318. FROM customermaster(NOLOCK) cust
  319. LEFT JOIN mobile_userRegistration(NOLOCK) v ON cust.customerId=v.customerId
  320. INNER JOIN OTPHistory otp(NOLOCK) ON cust.customerId=otp.customerId
  321. WHERE cust.customerId=@_customerId
  322. AND otp.codeType=@codeType
  323. AND v.imei=@imei
  324. AND otp.createdDate BETWEEN @today AND @today+ ' 23:59:59'
  325. IF ISNULL(@_count,0)>10
  326. BEGIN
  327. SELECT '1' ErrorCode, 'Your device has been blocked. Re-send code attempts exceeded from same device.' Msg ,NULL ID
  328. RETURN
  329. END
  330. ------------- ### Check attempts of re-send code ###ENDS------------
  331. INSERT INTO OTPHistory(username,OTP,OTP_Used,createdDate,codeType,customerId)
  332. SELECT @userName,@code,0,GETDATE(),@codeType,@_customerId
  333. SELECT '0' ErrorCode, 'Success' Msg ,@code ID
  334. RETURN
  335. END
  336. ELSE IF @flag='pwd-reset-rq' --password reset request(DONE)
  337. BEGIN
  338. ------------- ### Check if the user trying to re-send code is exists or not ###STARTS------------
  339. IF EXISTS(
  340. SELECT 'x' FROM dbo.CustomerMasterTemp(NOLOCK) cm
  341. WHERE (cm.email=@userName OR cm.mobile=@userName) AND ISNULL(cm.isDeleted,'N')='N'
  342. )
  343. BEGIN
  344. SELECT
  345. @_customerId=cm.customerId
  346. FROM dbo.CustomerMasterTemp(NOLOCK) cm
  347. WHERE cm.email=@userName OR cm.mobile=@userName
  348. INSERT INTO OTPHistory(username,OTP,OTP_Used,createdDate,codeType,customerId)
  349. SELECT @userName,@code,0,GETDATE(),'prc',@_customerId
  350. SELECT ErrorCode= '0'
  351. ,userId=@userName
  352. ,isVerified='false'
  353. RETURN
  354. END
  355. IF NOT EXISTS(
  356. SELECT 'x' FROM dbo.customerMaster(NOLOCK) cm
  357. WHERE (cm.email=@userName OR cm.mobile=@userName) AND ISNULL(cm.isDeleted,'N')='N'
  358. )
  359. BEGIN
  360. SELECT @_errorMsg = 'User with contact Info ' + @username + ' does not exists. If you are a new user, then sign up and proceed further.'
  361. SELECT '1' ErrorCode, @_errorMsg Msg ,NULL ID
  362. RETURN
  363. END
  364. ------------- ### Check if the user trying to re-send code is exists or not ###ENDS------------
  365. SELECT
  366. @_customerId=cm.customerId
  367. FROM dbo.customerMaster(NOLOCK) cm
  368. WHERE cm.email=@userName OR cm.mobile=@userName
  369. IF EXISTS(SELECT 'x'
  370. FROM customermaster(NOLOCK) cust
  371. WHERE cust.customerId=@_customerId AND ISNULL(cust.verifiedDate,'')<>''
  372. )
  373. BEGIN
  374. SELECT ErrorCode= '0'
  375. ,userId=@userName
  376. ,isVerified='true'
  377. RETURN
  378. END
  379. ELSE
  380. BEGIN
  381. INSERT INTO OTPHistory(username,OTP,OTP_Used,createdDate,codeType,customerId)
  382. SELECT @userName,@code,0,GETDATE(),'prc',@_customerId
  383. SELECT ErrorCode= '0'
  384. ,userId=@userName
  385. ,isVerified='false'
  386. RETURN
  387. END
  388. END
  389. ELSE IF @flag='qstn_validate' --reset password question validation(DONE)
  390. BEGIN
  391. IF ISNULL(@answer,'')=''
  392. BEGIN
  393. SELECT '1' ErrorCode, 'The answer field is required.' Msg ,NULL ID
  394. RETURN
  395. END
  396. ------------- ### Check if the user trying to validate is already exists ###STARTS------------
  397. IF NOT EXISTS(
  398. SELECT 'x' FROM dbo.customerMaster(NOLOCK) cm
  399. WHERE cm.email=@userName OR cm.mobile=@userName AND ISNULL(cm.isDeleted,'N')='N'
  400. )
  401. BEGIN
  402. SELECT @_errorMsg = 'User with contact Info ' + @username + ' does not exists. If you are a new user, then sign up and proceed further.'
  403. SELECT '1' ErrorCode, @_errorMsg Msg ,NULL ID
  404. RETURN
  405. END
  406. ------------- ### Check if the user trying to validate is already exists ###ENDS------------
  407. SELECT
  408. @_customerId=cm.customerId
  409. FROM dbo.customerMaster(NOLOCK) cm
  410. WHERE cm.email=@userName OR cm.mobile=@userName
  411. IF NOT EXISTS(
  412. SELECT 'x' FROM dbo.customerMaster(NOLOCK) cm
  413. WHERE cm.customerId=@_customerId AND ISNULL(cm.dob,'')=@answer
  414. )
  415. BEGIN
  416. SELECT '1' ErrorCode, 'Your Date of Birth does not matches with your saved details' Msg ,NULL ID
  417. RETURN
  418. END
  419. INSERT INTO OTPHistory(username,OTP,OTP_Used,createdDate,codeType,customerId)
  420. SELECT @userName,@code,0,GETDATE(),'prc',@_customerId
  421. SELECT '0' ErrorCode, 'Correct answer.' Msg ,@userName ID
  422. RETURN
  423. END
  424. ELSE IF @flag='prc-validate' --password recovery validation(DONE)
  425. BEGIN
  426. IF ISNULL(@code,'')=''
  427. BEGIN
  428. SELECT '1' ErrorCode, 'Password Recovery code is required.' Msg ,NULL ID
  429. RETURN
  430. END
  431. ------------- ### Check if the user trying to validate is already exists ###STARTS------------
  432. IF EXISTS(
  433. SELECT 'x' FROM dbo.CustomerMasterTemp(NOLOCK) cm
  434. WHERE cm.email=@userName OR cm.mobile=@userName AND ISNULL(cm.isDeleted,'N')='N'
  435. )
  436. BEGIN
  437. SELECT
  438. @_customerId=cm.customerId
  439. FROM dbo.customerMasterTemp(NOLOCK) cm
  440. WHERE cm.email=@userName OR cm.mobile=@userName
  441. SELECT TOP 1
  442. @_Otp=otp.OTP
  443. ,@_otpUsed=otp.OTP_Used
  444. ,@_createdDate=DATEADD(MINUTE,20,otp.createdDate)
  445. FROM dbo.OTPHistory(NOLOCK) otp
  446. WHERE otp.customerId=@_customerId AND otp.codeType='prc' /* AND otp.OTP=@code */
  447. ORDER BY otp.rowId DESC
  448. IF @_Otp=@code AND DATEDIFF(MINUTE, GETDATE() ,@_createdDate)>0
  449. AND @_otpUsed=0
  450. BEGIN
  451. --UPDATE OTPHistory
  452. -- SET OTP_Used = 1
  453. --WHERE username=@userName AND otp=@code
  454. UPDATE OTPHistory
  455. SET OTP_Used = 1
  456. WHERE customerId=@_customerId AND otp=@code
  457. SELECT '0' ErrorCode, 'Success' Msg, @userName ID
  458. RETURN
  459. END
  460. IF @_otpUsed=1
  461. BEGIN
  462. SELECT '1' ErrorCode, 'Password recovery code already used.' Msg ,NULL ID
  463. RETURN
  464. END
  465. ELSE IF ISNULL(@_Otp,'') <> @code
  466. BEGIN
  467. SELECT '1' ErrorCode, 'Invalid Password recovery code..' Msg ,NULL ID
  468. RETURN
  469. END
  470. ELSE IF DATEDIFF(MINUTE, GETDATE() ,@_createdDate)<=0
  471. BEGIN
  472. SELECT '1' ErrorCode, 'Password recovery code expired. Please re-send code and try again.' Msg ,NULL ID
  473. RETURN
  474. END
  475. ELSE
  476. BEGIN
  477. SELECT '1' ErrorCode, 'Invalid Password recovery code.' Msg ,NULL ID
  478. RETURN
  479. END
  480. END
  481. IF NOT EXISTS(
  482. SELECT 'x' FROM dbo.customerMaster(NOLOCK) cm
  483. WHERE cm.email=@username OR cm.mobile=@userName AND ISNULL(cm.isDeleted,'N')='N'
  484. )
  485. BEGIN
  486. SELECT @_errorMsg = 'User with contact Info ' + @username + ' does not exists. If you are a new user, then sign up and proceed further.'
  487. SELECT '1' ErrorCode, @_errorMsg Msg ,NULL ID
  488. RETURN
  489. END
  490. ------------- ### Check if the user trying to validate is already exists ###ENDS------------
  491. SELECT
  492. @_customerId=cm.customerId
  493. FROM dbo.customerMaster(NOLOCK) cm
  494. WHERE cm.email=@userName OR cm.mobile=@userName
  495. --SELECT TOP 1
  496. -- @_Otp=otp.OTP
  497. -- ,@_otpUsed=otp.OTP_Used
  498. -- ,@_createdDate=DATEADD(MINUTE,20,otp.createdDate)
  499. --FROM dbo.OTPHistory(NOLOCK) otp
  500. --WHERE otp.username=@userName AND otp.codeType='prc' AND otp.OTP=@code
  501. --ORDER BY otp.rowId DESC
  502. SELECT TOP 1
  503. @_Otp=otp.OTP
  504. ,@_otpUsed=otp.OTP_Used
  505. ,@_createdDate=DATEADD(MINUTE,20,otp.createdDate)
  506. FROM dbo.OTPHistory(NOLOCK) otp
  507. WHERE otp.customerId=@_customerId AND otp.codeType='prc' /* AND otp.OTP=@code */
  508. ORDER BY otp.rowId DESC
  509. IF @_Otp=@code AND DATEDIFF(MINUTE, GETDATE() ,@_createdDate)>0
  510. AND @_otpUsed=0
  511. BEGIN
  512. --UPDATE OTPHistory
  513. -- SET OTP_Used = 1
  514. --WHERE username=@userName AND otp=@code
  515. UPDATE OTPHistory
  516. SET OTP_Used = 1
  517. WHERE customerId=@_customerId AND otp=@code
  518. SELECT '0' ErrorCode, 'Success' Msg, @userName ID
  519. RETURN
  520. END
  521. IF @_otpUsed=1
  522. BEGIN
  523. SELECT '1' ErrorCode, 'Password recovery code already used.' Msg ,NULL ID
  524. RETURN
  525. END
  526. ELSE IF ISNULL(@_Otp,'') <> @code
  527. BEGIN
  528. SELECT '1' ErrorCode, 'Invalid Password recovery code..' Msg ,NULL ID
  529. RETURN
  530. END
  531. ELSE IF DATEDIFF(MINUTE, GETDATE() ,@_createdDate)<=0
  532. BEGIN
  533. SELECT '1' ErrorCode, 'Password recovery code expired. Please re-send code and try again.' Msg ,NULL ID
  534. RETURN
  535. END
  536. ELSE
  537. BEGIN
  538. SELECT '1' ErrorCode, 'Invalid Password recovery code.' Msg ,NULL ID
  539. RETURN
  540. END
  541. END
  542. ELSE IF @flag='u-pwd' --used to update user fields before login(DONE)
  543. BEGIN
  544. --SELECT TOP 1
  545. -- @_customerId=cust.customerId
  546. -- ,@_otpUsed=otp.OTP_Used
  547. -- ,@_onlineUser=cust.onlineUser
  548. --FROM customermaster(NOLOCK) cust
  549. --LEFT JOIN mobile_userRegistration(NOLOCK) v ON cust.customerId=v.customerId
  550. --INNER JOIN OTPHistory otp(NOLOCK) ON v.username=otp.username
  551. --WHERE cust.email=@userName OR cust.mobile=@userName AND ISNULL(cust.createdBy, '')=@scope
  552. --ORDER BY otp.rowId DESC
  553. IF EXISTS(
  554. SELECT 'x' FROM dbo.CustomerMasterTemp(NOLOCK) cm
  555. WHERE cm.email=@userName OR cm.mobile=@userName AND ISNULL(cm.isDeleted,'N')='N'
  556. )
  557. BEGIN
  558. SELECT TOP 1
  559. @_customerId=cust.customerId
  560. ,@_otpUsed=otp.OTP_Used
  561. ,@_onlineUser=cust.onlineUser
  562. ,@_createdBy=cust.createdBy
  563. FROM customermasterTemp(NOLOCK) cust
  564. LEFT JOIN mobile_userRegistration(NOLOCK) v ON cust.customerId=v.customerId
  565. LEFT JOIN OTPHistory(NOLOCK) otp ON cust.customerId=otp.customerId
  566. WHERE cust.email=@userName OR cust.mobile=@userName AND ISNULL(cust.createdBy, '')=@scope
  567. ORDER BY otp.rowId DESC
  568. IF ISNULL(@_customerId,'')=''
  569. BEGIN
  570. SELECT '1' ErrorCode, 'Invalid username.' Msg ,@userName ID
  571. RETURN
  572. END
  573. IF ISNULL(@Password,'')=''
  574. BEGIN
  575. SELECT '1' ErrorCode, 'The password field is Required.' Msg ,@userName ID
  576. RETURN
  577. END
  578. IF @_otpUsed<>1 AND @_createdBy IN ('mobile','mobile_app')
  579. BEGIN
  580. SELECT '1' ErrorCode, 'To update your password, please verify your account first with the verification code sent to your ' + @userName Msg ,@userName ID
  581. RETURN
  582. END
  583. UPDATE customermasterTemp SET
  584. customerpassword=ISNULL(dbo.FNAEncryptString(@Password),customerPassword)
  585. WHERE customerId=@_customerId
  586. SELECT
  587. errorCode='0'
  588. ,userId=@userName
  589. ,firstName=cm.firstName
  590. ,middleName=cm.middleName
  591. ,lastName=cm.lastName1
  592. ,nickName=cm.nickName
  593. ,email=ISNULL(cm.email,'')
  594. ,phone=ISNULL(cm.mobile,'')
  595. ,rewardPoint=CAST(ISNULL(cm.bonusPoint,0) AS DECIMAL)
  596. ,verificationCode=ISNULL(ur.OTP,'')
  597. ,VerificationCodeExpiryDate=''
  598. ,createdDate=ISNULL(ur.createdDate,'')
  599. ,userRoles=''
  600. ,active=CASE WHEN ISNULL(cm.isActive,'Y')='Y' THEN 1 ELSE 0 END
  601. ,kyc=CASE WHEN ISNULL(cm.createdDate,'') <> '' THEN 1 ELSE 0 END
  602. ,verified=CASE WHEN ISNULL(cm.verifiedDate,'') <> '' THEN 1 ELSE 0 END
  603. ,forgetCode=ISNULL(ur.passRecoveryCode,'')
  604. ,ForgetCodeExpiryDate=''
  605. ,primaryBankName=ISNULL(bl.BankName,'')
  606. ,walletNumber=ISNULL(cm.walletAccountNo,'')
  607. ,availableBalance=CAST([dbo].FNAGetCustomerACBal(@userName) AS DECIMAL)
  608. ,dpUrl=ISNULL(cm.dpUrl,'')
  609. ,cmRegistrationId=ISNULL(ur.cmRegistrationId,'')
  610. FROM dbo.customerMasterTemp(NOLOCK) cm
  611. LEFT JOIN dbo.mobile_userRegistration(NOLOCK) ur
  612. ON ur.customerId=cm.customerId
  613. LEFT JOIN dbo.vwBankLists bl WITH(NOLOCK)
  614. ON bl.bankCode=cm.bankName
  615. WHERE cm.customerId=@_customerId
  616. RETURN
  617. END
  618. SELECT TOP 1
  619. @_customerId=cust.customerId
  620. ,@_otpUsed=otp.OTP_Used
  621. ,@_onlineUser=cust.onlineUser
  622. FROM customermaster(NOLOCK) cust
  623. LEFT JOIN mobile_userRegistration(NOLOCK) v ON cust.customerId=v.customerId
  624. LEFT JOIN OTPHistory(NOLOCK) otp ON cust.customerId=otp.customerId
  625. WHERE cust.email=@userName OR cust.mobile=@userName AND ISNULL(cust.createdBy, '')=@scope
  626. ORDER BY otp.rowId DESC
  627. IF ISNULL(@_customerId,'')=''
  628. BEGIN
  629. SELECT '1' ErrorCode, 'Invalid username.' Msg ,@userName ID
  630. RETURN
  631. END
  632. IF ISNULL(@Password,'')=''
  633. BEGIN
  634. SELECT '1' ErrorCode, 'The password field is Required.' Msg ,@userName ID
  635. RETURN
  636. END
  637. IF @_otpUsed<>1 AND @_createdBy IN ('mobile','mobile_app')
  638. BEGIN
  639. SELECT '1' ErrorCode, 'To update your password, please verify your account first with the verification code sent to your ' + @userName Msg ,@userName ID
  640. RETURN
  641. END
  642. UPDATE customermaster SET
  643. customerpassword=ISNULL(dbo.FNAEncryptString(@Password),customerPassword)
  644. WHERE customerId=@_customerId
  645. SELECT
  646. errorCode='0'
  647. ,userId=@userName
  648. ,firstName=cm.firstName
  649. ,middleName=cm.middleName
  650. ,lastName=cm.lastName1
  651. ,nickName=''
  652. ,email=ISNULL(cm.email,'')
  653. ,phone=ISNULL(cm.mobile,'')
  654. ,rewardPoint=CAST(ISNULL(cm.bonusPoint,0) AS DECIMAL)
  655. ,verificationCode=ISNULL(ur.OTP,'')
  656. ,VerificationCodeExpiryDate=''
  657. ,createdDate=ISNULL(ur.createdDate,'')
  658. ,userRoles=''
  659. ,active=CASE WHEN ISNULL(cm.isActive,'Y')='Y' THEN 1 ELSE 0 END
  660. ,kyc=CASE WHEN ISNULL(cm.createdDate,'') <> '' THEN 1 ELSE 0 END
  661. ,verified=CASE WHEN ISNULL(cm.verifiedDate,'') <> '' THEN 1 ELSE 0 END
  662. ,forgetCode=ISNULL(ur.passRecoveryCode,'')
  663. ,ForgetCodeExpiryDate=''
  664. ,primaryBankName=ISNULL(bl.BankName,'')
  665. ,walletNumber=ISNULL(cm.walletAccountNo,'')
  666. ,availableBalance=CAST([dbo].FNAGetCustomerACBal(@userName) AS DECIMAL)
  667. ,dpUrl=''
  668. ,cmRegistrationId=ISNULL(ur.cmRegistrationId,'')
  669. FROM dbo.customerMaster(NOLOCK) cm
  670. LEFT JOIN dbo.mobile_userRegistration(NOLOCK) ur
  671. ON ur.customerId=cm.customerId
  672. LEFT JOIN dbo.vwBankLists bl WITH(NOLOCK)
  673. ON bl.bankCode=cm.bankName
  674. WHERE cm.customerId=@_customerId
  675. RETURN
  676. END
  677. ELSE IF @flag='u-user'
  678. BEGIN
  679. --SELECT TOP 1
  680. -- @_customerId=cust.customerId
  681. -- ,@_otpUsed=otp.OTP_Used
  682. -- ,@email=cust.email
  683. -- ,@MSISDN=cust.mobile
  684. --FROM customermaster(NOLOCK) cust
  685. --LEFT JOIN mobile_userRegistration(NOLOCK) v ON cust.customerId=v.customerId
  686. --INNER JOIN OTPHistory otp(NOLOCK) ON v.username=otp.username
  687. --WHERE cust.email=@userName OR cust.mobile=@userName AND ISNULL(cust.createdBy, '')=@scope
  688. --ORDER BY otp.rowId DESC
  689. SELECT TOP 1
  690. @_customerId=cust.customerId
  691. --,@_otpUsed=otp.OTP_Used
  692. ,@email=cust.email
  693. ,@MSISDN=cust.mobile
  694. FROM customermaster(NOLOCK) cust
  695. --LEFT JOIN mobile_userRegistration(NOLOCK) v ON cust.customerId=v.customerId
  696. INNER JOIN mobile_userRegistration(NOLOCK) v ON cust.customerId=v.customerId
  697. --LEFT JOIN OTPHistory(NOLOCK) otp ON cust.customerId=otp.customerId
  698. WHERE (cust.email=@userName OR cust.mobile=@userName)
  699. --AND ISNULL(cust.createdBy, '')=@scope
  700. --WHERE ((cust.email=@userName AND cust.isEmailVerified=1) OR (cust.mobile=@userName AND cust.isMobileVerified=1)) AND ISNULL(cust.createdBy, '')=@scope
  701. --ORDER BY otp.rowId DESC
  702. IF ISNULL(@_customerId,'')=''
  703. BEGIN
  704. SELECT '1' ErrorCode, 'Invalid username.' Msg ,@userName ID
  705. RETURN
  706. END
  707. IF ISNULL(@cmRegistrationId,'') <> ''
  708. BEGIN
  709. UPDATE mobile_userRegistration SET
  710. cmRegistrationId=ISNULL(@cmRegistrationId,cmRegistrationId)
  711. WHERE customerId=@_customerId
  712. END
  713. --IF ISNULL(@dpUrl,'') <> ''
  714. --BEGIN
  715. -- UPDATE cm SET dpUrl=ISNULL(@dpUrl,dpUrl)
  716. -- FROM dbo.customerMaster cm
  717. -- WHERE cm.customerId=@_customerId
  718. --END
  719. SELECT errorCode='0'
  720. ,userId=@userName
  721. ,firstName=cm.firstName
  722. ,middleName=cm.middleName
  723. ,lastName=cm.lastName1
  724. ,nickName=''
  725. ,email=ISNULL(cm.email,'')
  726. ,phone=ISNULL(cm.mobile,'')
  727. ,rewardPoint=CAST(ISNULL(cm.bonusPoint,0) AS DECIMAL)
  728. ,verificationCode=ISNULL(ur.OTP,'')
  729. ,VerificationCodeExpiryDate=''
  730. ,createdDate=ISNULL(ur.createdDate,'')
  731. ,userRoles=''
  732. ,active=CASE WHEN ISNULL(cm.isActive,'Y')='Y' THEN 1 ELSE 0 END
  733. ,kyc=CASE WHEN ISNULL(cm.createdDate,'') <> '' THEN 1 ELSE 0 END
  734. ,verified=CASE WHEN ISNULL(cm.verifiedDate,'') <> '' THEN 1 ELSE 0 END
  735. ,forgetCode=ISNULL(ur.passRecoveryCode,'')
  736. ,ForgetCodeExpiryDate=''
  737. ,primaryBankName=ISNULL(bl.BankName,'')
  738. ,walletNumber=ISNULL(cm.walletAccountNo,'')
  739. ,availableBalance=CAST([dbo].FNAGetCustomerACBal(@userName) AS DECIMAL)
  740. ,dpUrl=''
  741. ,cmRegistrationId=ISNULL(ur.cmRegistrationId,'')
  742. FROM dbo.mobile_userRegistration(NOLOCK) ur
  743. INNER JOIN dbo.customerMaster(NOLOCK) cm
  744. ON ur.customerId=cm.customerId
  745. LEFT JOIN dbo.vwBankLists(NOLOCK) bl
  746. ON bl.bankCode=cm.bankName
  747. WHERE cm.customerId=@_customerId
  748. RETURN
  749. END
  750. ELSE IF @flag='pwd-change' --password changed after successful login(DONE)
  751. BEGIN
  752. ------------- ### Check if the user trying to change password exists or not ###STARTS------------
  753. IF NOT EXISTS(SELECT 'x' FROM dbo.customerMaster(NOLOCK) cust
  754. WHERE cust.email=@userName OR cust.mobile=@userName AND ISNULL(cust.isDeleted,'N')='N'
  755. )
  756. BEGIN
  757. SELECT @_errorMsg = 'User with contact Info ' + @username + ' does not exists. If you are a new user, then sign up and proceed further.'
  758. SELECT '1' ErrorCode, @_errorMsg Msg ,NULL ID
  759. RETURN
  760. END
  761. ------------- ### Check if the user trying to change password exists or not ###ENDS------------
  762. SELECT
  763. @_customerId=cust.customerId
  764. ,@_password=cust.customerpassword
  765. FROM customermaster(NOLOCK) cust
  766. WHERE cust.email=@userName OR cust.mobile=@userName
  767. IF @_password <> dbo.FNAEncryptString(@Password)
  768. BEGIN
  769. EXEC proc_errorHandler 1, 'Incorrect existing password.. Please try again!', @userName
  770. RETURN;
  771. END
  772. IF @_password = dbo.FNAEncryptString(@newPassword)
  773. BEGIN
  774. EXEC proc_errorHandler 1, 'Please enter a new password.Old passwords are not accepted.', @userName
  775. RETURN;
  776. END
  777. IF ISNULL(@_customerId,'') = ''
  778. BEGIN
  779. SELECT @_errorMsg = 'User with contact Info ' + @username + ' does not exists. If you are a new user, then sign up and proceed further.'
  780. SELECT '1' ErrorCode, @_errorMsg Msg ,NULL ID
  781. RETURN
  782. END
  783. BEGIN TRANSACTION
  784. UPDATE customermaster SET
  785. customerpassword=dbo.FNAEncryptString(@newPassword)
  786. WHERE customerId=@_customerId
  787. IF @@TRANCOUNT > 0
  788. COMMIT TRANSACTION
  789. SELECT '0' ErrorCode, 'Password changed successfully.' Msg ,@userName ID
  790. RETURN
  791. END
  792. ELSE IF @flag='chk-referred'
  793. BEGIN
  794. IF EXISTS(SELECT 'x' FROM referralmaster(NOLOCK) WHERE email = @email OR mobile = @MSISDN)
  795. BEGIN
  796. SELECT @userName=userId FROM dbo.referralMaster(NOLOCK) WHERE email=@email OR mobile=@MSISDN
  797. SELECT errorCode = '0' ,
  798. referredBy=@userName,
  799. isReferred=1
  800. RETURN
  801. END
  802. ELSE
  803. BEGIN
  804. SELECT errorCode = '1',
  805. referredBy='',
  806. isReferred=0
  807. RETURN
  808. END
  809. END
  810. END TRY
  811. BEGIN CATCH
  812. IF @@TRANCOUNT > 0
  813. ROLLBACK TRANSACTION
  814. DECLARE @errorMessage VARCHAR(MAX)
  815. SET @errorMessage = ERROR_MESSAGE()
  816. SELECT '1' ErrorCode, @errorMessage Msg ,NULL ID
  817. END CATCH