forked from Shared/esg
database migrated to POSTGRESQL
This commit is contained in:
@@ -81,7 +81,7 @@ describe('unit helpers and mappers', () => {
|
||||
|
||||
it('maps user documents without leaking secrets', () => {
|
||||
const dto = UserMapper.toResponseDto({
|
||||
_id: { toString: () => 'user-1' },
|
||||
id: 'user-1',
|
||||
fullName: 'Admin User',
|
||||
username: 'admin',
|
||||
email: 'admin@example.com',
|
||||
|
||||
@@ -1,46 +1,42 @@
|
||||
import { IdempotencyService } from '../../src/idempotency/idempotency.service';
|
||||
import { IdempotencyRecordStatus } from '../../src/idempotency/schemas/idempotency-record.schema';
|
||||
|
||||
class FakeRecordModel {
|
||||
class FakeRecordRepository {
|
||||
private readonly records = new Map<string, any>();
|
||||
|
||||
findOne(filter: { scopeHash: string }) {
|
||||
return { exec: async () => this.records.get(filter.scopeHash) ?? null };
|
||||
create(record: any) {
|
||||
return { ...record };
|
||||
}
|
||||
|
||||
async create(record: any) {
|
||||
async findOne(options: { where: { scopeHash: string } }) {
|
||||
return this.records.get(options.where.scopeHash) ?? null;
|
||||
}
|
||||
|
||||
async save(record: any) {
|
||||
if (this.records.has(record.scopeHash)) {
|
||||
throw Object.assign(new Error('duplicate'), { code: 11000 });
|
||||
throw Object.assign(new Error('duplicate'), { code: '23505' });
|
||||
}
|
||||
const saved = { ...record, _id: record.scopeHash };
|
||||
const saved = { ...record, id: record.scopeHash };
|
||||
this.records.set(record.scopeHash, saved);
|
||||
return saved;
|
||||
}
|
||||
|
||||
updateOne(filter: { scopeHash: string; status: IdempotencyRecordStatus }, update: any) {
|
||||
return {
|
||||
exec: async () => {
|
||||
const record = this.records.get(filter.scopeHash);
|
||||
if (record?.status === filter.status) Object.assign(record, update.$set);
|
||||
return { acknowledged: true };
|
||||
},
|
||||
};
|
||||
async update(filter: { scopeHash: string; status: IdempotencyRecordStatus }, values: any) {
|
||||
const record = this.records.get(filter.scopeHash);
|
||||
if (record?.status === filter.status) Object.assign(record, values);
|
||||
return { affected: record ? 1 : 0 };
|
||||
}
|
||||
|
||||
deleteOne(filter: { scopeHash: string; status: IdempotencyRecordStatus }) {
|
||||
return {
|
||||
exec: async () => {
|
||||
const record = this.records.get(filter.scopeHash);
|
||||
if (record?.status === filter.status) this.records.delete(filter.scopeHash);
|
||||
return { acknowledged: true };
|
||||
},
|
||||
};
|
||||
async delete(filter: { scopeHash: string; status: IdempotencyRecordStatus }) {
|
||||
const record = this.records.get(filter.scopeHash);
|
||||
if (record?.status === filter.status) this.records.delete(filter.scopeHash);
|
||||
return { affected: record ? 1 : 0 };
|
||||
}
|
||||
}
|
||||
|
||||
describe('IdempotencyService', () => {
|
||||
it('replays completed responses and rejects body reuse', async () => {
|
||||
const service = new IdempotencyService(new FakeRecordModel() as never);
|
||||
const service = new IdempotencyService(new FakeRecordRepository() as never);
|
||||
const input = {
|
||||
key: 'request-1',
|
||||
callerId: 'user-1',
|
||||
@@ -62,7 +58,7 @@ describe('IdempotencyService', () => {
|
||||
});
|
||||
|
||||
it('rejects a duplicate while the original request is in progress', async () => {
|
||||
const service = new IdempotencyService(new FakeRecordModel() as never);
|
||||
const service = new IdempotencyService(new FakeRecordRepository() as never);
|
||||
const input = {
|
||||
key: 'request-2',
|
||||
callerId: 'user-1',
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
import { InquiryType } from '../../src/common/enums/inquiry-type.enum';
|
||||
import { TejaratNouProvider } from '../../src/providers/implementations/tejaratnou.provider';
|
||||
|
||||
function createProvider(inquiries?: Record<string, { url: string }>) {
|
||||
const getTejaratNouAccessToken = jest.fn().mockResolvedValue('tejarat-token');
|
||||
const config = {
|
||||
enabled: true,
|
||||
timeout: 15000,
|
||||
maxRetries: 1,
|
||||
baseUrl: 'https://accounts.tejaratnoins.ir',
|
||||
inquiryBaseUrl: 'https://gateway.tejaratnoins.ir',
|
||||
clientId: 'api-gateway',
|
||||
clientSecret: 'secret',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
inquiries,
|
||||
};
|
||||
const provider = new TejaratNouProvider(
|
||||
{ get: () => config } as never,
|
||||
{ getTejaratNouAccessToken } as never,
|
||||
{
|
||||
findPersonInquiry: jest.fn().mockResolvedValue(null),
|
||||
savePersonInquiry: jest.fn().mockResolvedValue(undefined),
|
||||
} as never,
|
||||
);
|
||||
const request = jest
|
||||
.spyOn((provider as unknown as { httpClient: { request: jest.Mock } }).httpClient, 'request')
|
||||
.mockResolvedValue({
|
||||
data: {
|
||||
isSuccess: true,
|
||||
data: { name: 'Ali', family: 'Ahmadi', nationalCodeString: '0820270725', ReturnValue: true },
|
||||
},
|
||||
});
|
||||
|
||||
return { provider, request };
|
||||
}
|
||||
|
||||
describe('Tejarat identity, shahkar, real estate, and sheba', () => {
|
||||
it('posts a Jalali birth year to birthdate-year', async () => {
|
||||
const { provider, request } = createProvider();
|
||||
|
||||
await provider.execute(
|
||||
InquiryType.PERSON,
|
||||
{ nationalCode: '4311402422', birthDate: '1378' },
|
||||
{ requestId: 'req-year', trackingCode: 'trk-year' },
|
||||
);
|
||||
|
||||
expect(request).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
url: '/api/identity-inquiry/national-code/4311402422/birthdate-year/1378',
|
||||
headers: expect.objectContaining({ Authorization: 'Bearer tejarat-token', Accept: 'text/plain' }),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('gets a full Jalali birth date as a Gregorian birthdate', async () => {
|
||||
const { provider, request } = createProvider();
|
||||
|
||||
await provider.execute(
|
||||
InquiryType.PERSON,
|
||||
{ nationalCode: '0820270725', birthDate: '1373-10-12' },
|
||||
{ requestId: 'req-date', trackingCode: 'trk-date' },
|
||||
);
|
||||
|
||||
expect(request).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
method: 'GET',
|
||||
url: '/api/identity-inquiry/national-code/0820270725/birthdate/1995-01-02',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('posts sheba with the Tejarat account, national id, and sheba fields', async () => {
|
||||
const { provider, request } = createProvider({
|
||||
[InquiryType.SHEBA]: {
|
||||
url: 'https://gateway.tejaratnoins.ir/api/central-insurance-sheba-inquiry',
|
||||
},
|
||||
});
|
||||
|
||||
await provider.execute(
|
||||
InquiryType.SHEBA,
|
||||
{
|
||||
nationalCode: '0012345678',
|
||||
sheba: 'IR090540202620101368196602',
|
||||
accountOwnerType: '1',
|
||||
},
|
||||
{ requestId: 'req-sheba', trackingCode: 'trk-sheba' },
|
||||
);
|
||||
|
||||
expect(request).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
url: 'https://gateway.tejaratnoins.ir/api/central-insurance-sheba-inquiry',
|
||||
data: {
|
||||
AccountOwnerType: 1,
|
||||
NationalId: '0012345678',
|
||||
ShebaId: 'IR090540202620101368196602',
|
||||
},
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,72 @@
|
||||
import { InquiryType } from '../../src/common/enums/inquiry-type.enum';
|
||||
import { TejaratNouProvider } from '../../src/providers/implementations/tejaratnou.provider';
|
||||
|
||||
describe('Tejarat policy by plate', () => {
|
||||
it('posts plate parts and national code to central-insurance-car-inquiry with the Tejarat token', async () => {
|
||||
const getTejaratNouAccessToken = jest.fn().mockResolvedValue('tejarat-token');
|
||||
const config = {
|
||||
enabled: true,
|
||||
timeout: 15000,
|
||||
maxRetries: 1,
|
||||
baseUrl: 'https://accounts.tejaratnoins.ir',
|
||||
inquiryBaseUrl: 'https://gateway.tejaratnoins.ir',
|
||||
clientId: 'api-gateway',
|
||||
clientSecret: 'secret',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
};
|
||||
const provider = new TejaratNouProvider(
|
||||
{ get: () => config } as never,
|
||||
{ getTejaratNouAccessToken } as never,
|
||||
{} as never,
|
||||
);
|
||||
const request = jest.spyOn((provider as unknown as { httpClient: { request: jest.Mock } }).httpClient, 'request').mockResolvedValue({
|
||||
data: {
|
||||
isSuccess: true,
|
||||
data: { PolicyNo: 'P-1', NtnlId: '4311402422' },
|
||||
},
|
||||
});
|
||||
|
||||
const result = await provider.execute(
|
||||
InquiryType.POLICY_BY_PLATE,
|
||||
{
|
||||
nationalCode: '4311402422',
|
||||
plk1: '12',
|
||||
plk2: 'ب',
|
||||
plk3: '345',
|
||||
plksrl: '67',
|
||||
},
|
||||
{ requestId: 'req-1', trackingCode: 'trk-1' },
|
||||
);
|
||||
|
||||
expect(getTejaratNouAccessToken).toHaveBeenCalledWith(config, expect.any(AbortSignal));
|
||||
expect(request).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
url: '/api/central-insurance-car-inquiry',
|
||||
data: {
|
||||
Part1: '12',
|
||||
Part2: 'ب',
|
||||
Part3: '345',
|
||||
Part4: '67',
|
||||
NationalCode: '4311402422',
|
||||
},
|
||||
headers: {
|
||||
Authorization: 'Bearer tejarat-token',
|
||||
Accept: 'text/plain',
|
||||
},
|
||||
}),
|
||||
);
|
||||
expect(result).toEqual({
|
||||
raw: {
|
||||
nationalCode: '4311402422',
|
||||
plk1: '12',
|
||||
plk2: 'ب',
|
||||
plk3: '345',
|
||||
plksrl: '67',
|
||||
PolicyNo: 'P-1',
|
||||
NtnlId: '4311402422',
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
import {
|
||||
getCurrentJalaliYear,
|
||||
isValidPlateLetter,
|
||||
mergePlateAndNationalPolicies,
|
||||
normalizeSoapCarPolicy,
|
||||
selectBestThirdPartyPolicy,
|
||||
} from '../../src/common/helpers/third-party-car-rules.helper';
|
||||
|
||||
describe('third-party car rules', () => {
|
||||
it('rejects invalid plate letters', () => {
|
||||
expect(isValidPlateLetter('')).toBe(false);
|
||||
expect(isValidPlateLetter('ب')).toBe(true);
|
||||
});
|
||||
|
||||
it('prefers active newer policy when multiple acceptable policies exist', () => {
|
||||
const older = normalizeSoapCarPolicy(
|
||||
{
|
||||
NtnlId: '4311402422',
|
||||
VhcleGrpCd: '2',
|
||||
PlcyBegnDt: '20260101',
|
||||
PlcyEndDt: '20270101',
|
||||
},
|
||||
'4311402422',
|
||||
);
|
||||
const newer = normalizeSoapCarPolicy(
|
||||
{
|
||||
NtnlId: '4311402422',
|
||||
VhcleGrpCd: '3',
|
||||
PlcyBegnDt: '20260701',
|
||||
PlcyEndDt: '20270701',
|
||||
},
|
||||
'4311402422',
|
||||
);
|
||||
|
||||
const selected = selectBestThirdPartyPolicy([older, newer], '4311402422');
|
||||
expect(selected?.raw.PlcyBegnDt).toBe('20260701');
|
||||
});
|
||||
|
||||
it('keeps plate result when national code points to another vehicle', () => {
|
||||
const plate = normalizeSoapCarPolicy(
|
||||
{
|
||||
NtnlId: '4311402422',
|
||||
ChassisNo: 'CHASSIS-A',
|
||||
Plk1: '12',
|
||||
Plk2: 'ب',
|
||||
Plk3: '345',
|
||||
PlkSrl: '67',
|
||||
},
|
||||
'4311402422',
|
||||
);
|
||||
const national = normalizeSoapCarPolicy(
|
||||
{
|
||||
NtnlId: '4311402422',
|
||||
ChassisNo: 'CHASSIS-B',
|
||||
Plk1: '99',
|
||||
Plk2: 'ج',
|
||||
Plk3: '111',
|
||||
PlkSrl: '22',
|
||||
},
|
||||
'4311402422',
|
||||
);
|
||||
|
||||
const merged = mergePlateAndNationalPolicies(plate, national);
|
||||
expect(merged?.chassisNo).toBe('CHASSIS-A');
|
||||
});
|
||||
|
||||
it('returns current jalali year', () => {
|
||||
expect(getCurrentJalaliYear(new Date('2026-03-21T00:00:00Z'))).toBeGreaterThan(1400);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user