forked from Shared/esg
database migrated to POSTGRESQL
This commit is contained in:
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user