Skip to content

Commit

Permalink
Refactor function into class
Browse files Browse the repository at this point in the history
  • Loading branch information
SethO committed Oct 31, 2023
1 parent 0f025ac commit 9951b88
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
17 changes: 8 additions & 9 deletions lib/keyValueRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class KeyValueRepository {
}

async update(item: any) {
validateHashKeyPropertyExists({ item, keyName: this.keyName });
this.validateHashKeyPropertyExists(item);
const { revision: previousRevision } = item;
const itemToSave = setRepositoryModifiedProperties(item);
const putParams: PutCommandInput = {
Expand Down Expand Up @@ -160,7 +160,7 @@ class KeyValueRepository {
}

async updatePartial(item: any): Promise<Record<string, any>> {
validateHashKeyPropertyExists({ item, keyName: this.keyName });
this.validateHashKeyPropertyExists(item);
const updateInput = this.buildUpdateCommandInput(item);
let result: UpdateCommandOutput;
try {
Expand Down Expand Up @@ -211,6 +211,12 @@ class KeyValueRepository {

return updateInput;
}

private validateHashKeyPropertyExists(item: any) {
if (!item[this.keyName]) {
throw new BadRequest(`Bad Request: Item has no key named "${this.keyName}"`);
}
}
}

const isNotFoundConflict = (itemFromError?: any) => !itemFromError;
Expand All @@ -221,13 +227,6 @@ const isRevisionConflict = (input: { expectedRevision: number; actualRevision: n
return expectedRevision !== actualRevision;
};

const validateHashKeyPropertyExists = (input: { item: any; keyName: string }) => {
const { item, keyName } = input;
if (!item[keyName]) {
throw new BadRequest(`Bad Request: Item has no key named "${keyName}"`);
}
};

const setRepositoryModifiedProperties = (item: any) => {
const returnItem = { ...item };
returnItem.updatedAt = new Date().toISOString();
Expand Down
2 changes: 1 addition & 1 deletion test/updatePartial.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import getDocumentClient from './documentClient';
const TableName = 'HashKeyTestDB';
const KeyName = 'key';

describe('When updating an item', () => {
describe('When partially updating an item', () => {
const testKeys: string[] = [];
const documentClient = getDocumentClient();

Expand Down

0 comments on commit 9951b88

Please sign in to comment.