{"size": 720, "ext": "js", "max_stars_repo_path": "test/support/app/abilities/buyer_ability.js", "max_stars_repo_name": "caiena/abilities.js", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/support/app/abilities/buyer_ability.js", "max_issues_repo_name": "caiena/abilities.js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/support/app/abilities/buyer_ability.js", "max_forks_repo_name": "caiena/abilities.js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.6923076923, "max_line_length": 73, "alphanum_fraction": 0.5986111111}
import { AsyncAbilityBuilder } from '../../../../src'


const BuyerAbility = {
  build(user, options = {}) {
    // simulating async
    const wait = ms => new Promise(resolve => setTimeout(resolve, ms));

    let ability = AsyncAbilityBuilder.define((can, cannot) => {
      can('create', 'Purchase')
      can('index', 'Purchase')
      can('read', 'Purchase', async (purchase) => {
        // simulating a fetch to request data (async)
        return wait().then(() => purchase.createdById === user.id)
      })
      can('delete', 'Purchase', async (purchase) => !purchase.approved)
      cannot('delete', 'Purchase', async (purchase) => purchase.approved)
    })

    return ability
  }
}


export { BuyerAbility }
