Skip to content

Commit

Permalink
Merge pull request #20 from imRohan/issue/19
Browse files Browse the repository at this point in the history
Fixes Basket Retrieval
  • Loading branch information
Rohan Likhite committed May 2, 2020
2 parents ce04777 + c3b69b3 commit 4b4b37d
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/controllers/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AccountController {
const { contactEmail } = params
await mailer.sendWelcomeEmail(contactEmail, _accountUUID)

logger.logAndSlack(`Account created for ${contactEmail}`)
logger.logAndSlack(`Account created for ${contactEmail}: ${_accountUUID}`)
return _accountUUID
} catch (error) {
logger.error(`Account creation failed: ${error.message}`)
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BlockController {
throw new Error('max number of blocks reached')
}

logger.info('Creating new block in account: ${accountUUID}')
logger.info(`Creating new block in account: ${accountUUID}`)
const _block = new Block(accountUUID, name, payload)
await _block.store()

Expand Down
2 changes: 1 addition & 1 deletion src/models/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Account {

public async getBlocks(): Promise<string[]> {
const _accountKey = Account.generateRedisKey(this.uuid)
const _blocks = await dataStore.scan(`${_accountKey}::block:*`, this.maxNumberOfBlocks)
const _blocks = await dataStore.find(`${_accountKey}::block:*`)

const _blocksSanitized = _blocks.map((block) => {
return block.split(':')[4]
Expand Down
2 changes: 1 addition & 1 deletion src/services/__mocks__/dataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const dataStore = {
get: jest.fn(async () => { return }),
set: jest.fn(async () => { return }),
delete: jest.fn(async () => { return }),
scan: jest.fn(async () => { return }),
find: jest.fn(async () => { return }),
}

export = dataStore
8 changes: 4 additions & 4 deletions src/services/dataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ const dataStore = {
}
},

async scan(pattern: string, count: number): Promise<string[]> {
async find(pattern: string): Promise<string[]> {
try {
const _redisClient = redis.createClient()
const _scan = promisify(_redisClient.scan).bind(_redisClient)
const [ , _storedKeys ] = await _scan(0, 'MATCH', pattern, 'COUNT', count)
const _keys = promisify(_redisClient.keys).bind(_redisClient)
const _storedKeys = await _keys(pattern)
_redisClient.quit()

return _storedKeys
} catch (error) {
logger.error(`Error when scanning keys: ${error.message}`)
logger.error(`Error when finding keys: ${error.message}`)
throw new Error('Pantry is having critical issues')
}
},
Expand Down
6 changes: 3 additions & 3 deletions tests/controllers/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('When retrieving an account', () => {

it ('returns the correct account attributes', async () => {
mockedDataStore.get.mockReturnValueOnce(Promise.resolve(JSON.stringify(_existingAccount)))
mockedDataStore.scan.mockReturnValueOnce(Promise.resolve([]))
mockedDataStore.find.mockReturnValueOnce(Promise.resolve([]))

const _accountBase: IAccount = await AccountController.get(_existingAccount.uuid)
expect(_accountBase).toBeDefined()
Expand All @@ -94,7 +94,7 @@ describe('When deleting an account', () => {

it ('returns confirmation message', async () => {
mockedDataStore.get.mockReturnValueOnce(Promise.resolve(JSON.stringify(_existingAccount)))
mockedDataStore.scan.mockReturnValueOnce(Promise.resolve([]))
mockedDataStore.find.mockReturnValueOnce(Promise.resolve([]))

const _response = await AccountController.delete(_existingAccount.uuid)
expect(_response).toMatch(/Your Pantry has been deleted/)
Expand All @@ -105,7 +105,7 @@ describe('When deleting an account', () => {
.mockReturnValueOnce(Promise.resolve(JSON.stringify(_existingAccount)))
.mockReturnValueOnce(Promise.resolve(JSON.stringify({ derp: 'flerp' })))

mockedDataStore.scan.mockReturnValueOnce(Promise.resolve(['existingBlock']))
mockedDataStore.find.mockReturnValueOnce(Promise.resolve(['existingBlock']))

const _response = await AccountController.delete(_existingAccount.uuid)
expect(_response).toMatch(/Your Pantry has been deleted/)
Expand Down
6 changes: 3 additions & 3 deletions tests/controllers/block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('When creating a block', () => {
const _accountUUID = '6dc70531-d0bf-4b3a-8265-b20f8a69e180'

mockedDataStore.get.mockReturnValueOnce(Promise.resolve(JSON.stringify(_existingAccount)))
mockedDataStore.scan.mockReturnValueOnce(Promise.resolve([]))
mockedDataStore.find.mockReturnValueOnce(Promise.resolve([]))

const _response = await BlockController.create(_accountUUID, 'NewBlock', { derp: 'flerp' })
expect(_response).toMatch(/Your Pantry was updated with basket: NewBlock/)
Expand All @@ -37,7 +37,7 @@ describe('When creating a block', () => {
const _accountUUID = '6dc70531-d0bf-4b3a-8265-b20f8a69e180'

mockedDataStore.get.mockReturnValueOnce(Promise.resolve(JSON.stringify(_existingAccount)))
mockedDataStore.scan.mockReturnValueOnce(Promise.resolve([]))
mockedDataStore.find.mockReturnValueOnce(Promise.resolve([]))

await expect(BlockController.create(_accountUUID, 'NewBlock', {}))
.rejects
Expand All @@ -56,7 +56,7 @@ describe('When creating a block', () => {
}

mockedDataStore.get.mockReturnValueOnce(Promise.resolve(JSON.stringify(_maxedAccount)))
mockedDataStore.scan.mockReturnValueOnce(Promise.resolve(['oldBlock']))
mockedDataStore.find.mockReturnValueOnce(Promise.resolve(['oldBlock']))

await expect(BlockController.create(_accountUUID, 'NewBlock', { derp: 'flerp' }))
.rejects
Expand Down

0 comments on commit 4b4b37d

Please sign in to comment.