5
5
using StrDss . Common ;
6
6
using StrDss . Data . Entities ;
7
7
using StrDss . Model ;
8
+ using System . Text . RegularExpressions ;
8
9
9
10
namespace StrDss . Data . Repositories
10
11
{
@@ -14,7 +15,7 @@ public interface IBizLicenseRepository
14
15
Task CreateBizLicTempTable ( ) ;
15
16
Task InsertRowToBizLicTempTable ( BizLicenseRowUntyped row , long providingOrganizationId ) ;
16
17
Task ProcessBizLicTempTable ( ) ;
17
- Task < long ? > GetMatchingBusinessLicenseId ( long orgId , string effectiveBizLicNo ) ;
18
+ Task < ( long ? , string ? ) > GetMatchingBusinessLicenseIdAndNo ( long orgId , string effectiveBizLicNo ) ;
18
19
}
19
20
20
21
public class BizLicenseRepository : RepositoryBase < DssBusinessLicence > , IBizLicenseRepository
@@ -143,13 +144,32 @@ public async Task ProcessBizLicTempTable()
143
144
await _dbContext . Database . ExecuteSqlRawAsync ( "CALL dss_process_biz_lic_table();" ) ;
144
145
}
145
146
146
- public async Task < long ? > GetMatchingBusinessLicenseId ( long orgId , string effectiveBizLicNo )
147
- {
148
- //todo: need to use effectiveBizLicNo from the table
149
- var bizLic = await _dbSet . AsNoTracking ( )
150
- . FirstOrDefaultAsync ( x => x . ProvidingOrganizationId == orgId && x . BusinessLicenceNo == effectiveBizLicNo ) ;
147
+ //public async Task<(long?, string?)> GetMatchingBusinessLicenseIdAndNo(long orgId, string effectiveBizLicNo)
148
+ //{
149
+ // var license = await _dbSet.AsNoTracking()
150
+ // .Where(x => x.ProvidingOrganizationId == orgId && x.BusinessLicenceNo == effectiveBizLicNo)
151
+ // .Select(x => new { x.BusinessLicenceId, x.BusinessLicenceNo })
152
+ // .FirstOrDefaultAsync();
153
+
154
+ // return license == null ? (null, null) : (license.BusinessLicenceId, CommonUtils.SanitizeAndUppercaseString(license.BusinessLicenceNo));
155
+ //}
151
156
152
- return bizLic == null ? null : bizLic . BusinessLicenceId ;
157
+ public async Task < ( long ? , string ? ) > GetMatchingBusinessLicenseIdAndNo ( long orgId , string effectiveBizLicNo )
158
+ {
159
+ // Raw SQL query using PostgreSQL regexp_replace to remove non-alphanumeric characters in the database query
160
+ var sqlQuery = @"
161
+ SELECT business_licence_id, business_licence_no
162
+ FROM dss_business_licence
163
+ WHERE providing_organization_id = @orgId
164
+ AND regexp_replace(business_licence_no, '[^a-zA-Z0-9]', '', 'g') = @sanitizedBizLicNo
165
+ LIMIT 1" ;
166
+
167
+ var license = await _dbContext . DssBusinessLicences
168
+ . FromSqlRaw ( sqlQuery , new NpgsqlParameter ( "orgId" , orgId ) , new NpgsqlParameter ( "sanitizedBizLicNo" , effectiveBizLicNo ) )
169
+ . Select ( x => new { x . BusinessLicenceId , x . BusinessLicenceNo } )
170
+ . FirstOrDefaultAsync ( ) ;
171
+
172
+ return license == null ? ( null , null ) : ( license . BusinessLicenceId , CommonUtils . SanitizeAndUppercaseString ( license . BusinessLicenceNo ) ) ;
153
173
}
154
174
}
155
175
}
0 commit comments