Skip to content

Commit

Permalink
fix(assist): token limit
Browse files Browse the repository at this point in the history
  • Loading branch information
dalssoft committed Jun 15, 2023
1 parent 97eff79 commit 662603f
Showing 1 changed file with 0 additions and 120 deletions.
120 changes: 0 additions & 120 deletions src/templates/assist/usecase.codex.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -179,126 +179,6 @@ module.exports =
.usecase
END

Example: `Find User`
Input:
~~~~
const User = require('../../entities/user')
const findUser = require('./findUser')
const assert = require('assert')
const { spec, scenario, given, check, samples } = require('@herbsjs/herbs').specs
const { herbarium } = require('@herbsjs/herbarium')

const findUserSpec = spec({

usecase: findUser,

'Find a user when it exists': scenario({
'Given an existing user': given({
request: {
id: 'a text'
},
user: { hasAccess: true },
injection: {
UserRepository: class UserRepository {
async findByID(id) {
const fakeUser = {
id: 'a text',
nickname: 'a text',
registrationNumber: 99,
password: 'a text'
}
return ([User.fromJSON(fakeUser)])
}
}
},
}),

// when: default when for use case

'Must run without errors': check((ctx) => {
assert.ok(ctx.response.isOk)
}),

'Must return a valid user': check((ctx) => {
assert.strictEqual(ctx.response.ok.isValid(), true)
})

}),

'Do not find a user when it does not exist': scenario({
'Given an empty user repository': given({
request: {
id: 'a text'
},
user: { hasAccess: true },
injection: {
UserRepository: class UserRepository {
async findByID(id) { return [] }
}
},
}),

// when: default when for use case

'Must return an error': check((ctx) => {
assert.ok(ctx.response.isErr)
assert.ok(ctx.response.isNotFoundError)
}),
}),
})

module.exports =
herbarium.specs
.add(findUserSpec, 'FindUserSpec')
.metadata({ usecase: 'FindUser' })
.usecase
~~~~

Output:
~~~~
const { usecase, step, Ok, Err } = require('@herbsjs/herbs')
const { herbarium } = require('@herbsjs/herbarium')
const User = require('../../entities/user')
const UserRepository = require('../../../infra/data/repositories/userRepository')

const dependency = { UserRepository }

const findUser = injection =>
usecase('Find a User', {
// Input/Request metadata and validation
request: {
id: String
},

// Output/Response metadata
response: User,

//Authorization with Audit
// authorize: (user) => (user.canFindOneUser ? Ok() : Err()),
authorize: () => Ok(),

setup: ctx => (ctx.di = Object.assign({}, dependency, injection)),

'Find and return the User': step(async ctx => {
const id = ctx.req.id
const repo = new ctx.di.UserRepository(injection)
const [user] = await repo.findByID(id)
if (!user) return Err.notFound({
message: `User entity not found by ID: ${id}`,
payload: { entity: 'User', id }
})
// ctx.ret is the return value of a use case
return Ok(ctx.ret = user)
})
})

module.exports =
herbarium.usecases
.add(findUser, 'FindUser')
.metadata({ group: 'User', operation: herbarium.crud.read, entity: User })
.usecase
END

Example: `<%- props.usecaseName %>`
Input:
~~~~
Expand Down

0 comments on commit 662603f

Please sign in to comment.