Skip to content

Commit

Permalink
fix: set updateAt on records when updating a record (openwallet-found…
Browse files Browse the repository at this point in the history
…ation#1272)

Signed-off-by: KolbyRKunz <[email protected]>
  • Loading branch information
KolbyRKunz authored Feb 10, 2023
1 parent 1d487b1 commit 2669d7d
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/askar/src/storage/AskarStorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class AskarStorageService<T extends BaseRecord> implements StorageService
assertAskarWallet(agentContext.wallet)
const session = agentContext.wallet.session

record.updatedAt = new Date()

const value = JsonTransformer.serialize(record)
const tags = transformFromRecordTagValues(record.getTags()) as Record<string, string>

Expand All @@ -40,6 +42,8 @@ export class AskarStorageService<T extends BaseRecord> implements StorageService
assertAskarWallet(agentContext.wallet)
const session = agentContext.wallet.session

record.updatedAt = new Date()

const value = JsonTransformer.serialize(record)
const tags = transformFromRecordTagValues(record.getTags()) as Record<string, string>

Expand Down
12 changes: 12 additions & 0 deletions packages/askar/src/storage/__tests__/AskarStorageService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { AskarWallet } from '../../wallet/AskarWallet'
import { AskarStorageService } from '../AskarStorageService'
import { askarQueryFromSearchQuery } from '../utils'

const startDate = Date.now()

describe('AskarStorageService', () => {
let wallet: AskarWallet
let storageService: AskarStorageService<TestRecord>
Expand Down Expand Up @@ -127,6 +129,11 @@ describe('AskarStorageService', () => {

expect(record).toEqual(found)
})

it('updatedAt should have a new value after a save', async () => {
const record = await insertRecord({ id: 'test-id' })
expect(record.updatedAt?.getTime()).toBeGreaterThan(startDate)
})
})

describe('getById()', () => {
Expand Down Expand Up @@ -165,6 +172,11 @@ describe('AskarStorageService', () => {
const retrievedRecord = await storageService.getById(agentContext, TestRecord, record.id)
expect(retrievedRecord).toEqual(record)
})

it('updatedAt should have a new value after an update', async () => {
const record = await insertRecord({ id: 'test-id' })
expect(record.updatedAt?.getTime()).toBeGreaterThan(startDate)
})
})

describe('delete()', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import base from '../../jest.config.base'

import packageJson from './package.json'

process.env.TZ = 'GMT'

const config: Config.InitialOptions = {
...base,
name: packageJson.name,
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/storage/IndyStorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export class IndyStorageService<T extends BaseRecord<any, any, any>> implements
public async save(agentContext: AgentContext, record: T) {
assertIndyWallet(agentContext.wallet)

record.updatedAt = new Date()

const value = JsonTransformer.serialize(record)
const tags = this.transformFromRecordTagValues(record.getTags()) as Record<string, string>

Expand All @@ -157,6 +159,8 @@ export class IndyStorageService<T extends BaseRecord<any, any, any>> implements
public async update(agentContext: AgentContext, record: T): Promise<void> {
assertIndyWallet(agentContext.wallet)

record.updatedAt = new Date()

const value = JsonTransformer.serialize(record)
const tags = this.transformFromRecordTagValues(record.getTags()) as Record<string, string>

Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/storage/__tests__/IndyStorageService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import type * as Indy from 'indy-sdk'
import { agentDependencies, getAgentConfig, getAgentContext } from '../../../tests/helpers'
import { SigningProviderRegistry } from '../../crypto/signing-provider'
import { RecordDuplicateError, RecordNotFoundError } from '../../error'
import { sleep } from '../../utils/sleep'
import { IndyWallet } from '../../wallet/IndyWallet'
import { IndyStorageService } from '../IndyStorageService'

import { TestRecord } from './TestRecord'

const startDate = Date.now()

describe('IndyStorageService', () => {
let wallet: IndyWallet
let indy: typeof Indy
Expand Down Expand Up @@ -113,6 +116,12 @@ describe('IndyStorageService', () => {

expect(record).toEqual(found)
})

it('After a save the record should have update the updatedAt property', async () => {
const time = startDate
const record = await insertRecord({ id: 'test-updatedAt' })
expect(record.updatedAt?.getTime()).toBeGreaterThan(time)
})
})

describe('getById()', () => {
Expand Down Expand Up @@ -151,6 +160,18 @@ describe('IndyStorageService', () => {
const retrievedRecord = await storageService.getById(agentContext, TestRecord, record.id)
expect(retrievedRecord).toEqual(record)
})

it('After a record has been updated it should have updated the updatedAT property', async () => {
const time = startDate
const record = await insertRecord({ id: 'test-id' })

record.replaceTags({ ...record.getTags(), foo: 'bar' })
record.foo = 'foobaz'
await storageService.update(agentContext, record)

const retrievedRecord = await storageService.getById(agentContext, TestRecord, record.id)
expect(retrievedRecord.createdAt.getTime()).toBeGreaterThan(time)
})
})

describe('delete()', () => {
Expand Down
Loading

0 comments on commit 2669d7d

Please sign in to comment.