Skip to content

Commit a237cf9

Browse files
feat: added delete record
1 parent 94289ee commit a237cf9

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

packages/idempotency/src/persistence/PersistenceLayer.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,19 @@ abstract class PersistenceLayer implements PersistenceLayerInterface {
2929
this.functionName = this.getEnvVarsService().getLambdaFunctionName() + '.' + functionName;
3030
}
3131

32-
public async deleteRecord(): Promise<void> { }
33-
32+
public async deleteRecord(data: unknown): Promise<void> {
33+
const idempotencyRecord: IdempotencyRecord =
34+
new IdempotencyRecord(this.getHashedIdempotencyKey(data),
35+
IdempotencyRecordStatus.EXPIRED,
36+
undefined,
37+
undefined,
38+
undefined,
39+
undefined
40+
);
41+
42+
this._deleteRecord(idempotencyRecord);
43+
}
44+
3445
public async getRecord(data: unknown): Promise<IdempotencyRecord> {
3546
const idempotencyKey: string = this.getHashedIdempotencyKey(data);
3647

packages/idempotency/tests/unit/PersistenceLayer.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,32 @@ describe('Class: Persistence Layer', ()=> {
197197
expect(getRecord).toHaveBeenCalledWith(expectedIdempotencyKey);
198198
});
199199
});
200+
201+
describe('Method: deleteRecord', ()=> {
202+
beforeEach(()=> {
203+
putRecord.mockClear();
204+
(createHash as jest.MockedFunction<typeof createHash>).mockReturnValue(
205+
{
206+
update: cryptoUpdateMock,
207+
digest: cryptoDigestMock.mockReturnValue(mockDigest)
208+
} as unknown as Hash
209+
);
210+
});
211+
212+
test('When called it deletes the record with the idempotency key for the data passed in', ()=> {
213+
const persistenceLayer: PersistenceLayer = new PersistenceLayerTestClass();
214+
const data = 'someData';
215+
const lambdaFunctionName = 'LambdaName';
216+
jest.spyOn(EnvironmentVariablesService.prototype, 'getLambdaFunctionName').mockReturnValue(lambdaFunctionName);
217+
218+
const functionName = 'functionName';
219+
const expectedIdempotencyKey = lambdaFunctionName + '.' + functionName + '#' + mockDigest;
220+
persistenceLayer.configure(functionName);
221+
222+
persistenceLayer.deleteRecord(data);
223+
const deletedIdempotencyRecord: IdempotencyRecord = deleteRecord.mock.calls[0][0];
224+
225+
expect(deletedIdempotencyRecord.idempotencyKey).toEqual(expectedIdempotencyKey);
226+
});
227+
});
200228
});

0 commit comments

Comments
 (0)