Skip to content

Commit

Permalink
Refactor prepare_ents() eventbrite-provider.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
guhmerces committed Feb 9, 2022
1 parent f915b73 commit 9486308
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
61 changes: 28 additions & 33 deletions src/eventbrite-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Sdk } from 'eventbrite/lib/types'
import { entities } from './entities'
import { make_actions } from './make-actions'
import { make_request } from './make-request'
import { ActionData, EntityMap } from './types'
import { EntData, EntityMap } from './types'

type EventbriteProviderOptions = {}

Expand All @@ -20,57 +20,52 @@ function EventbriteProvider(this: any, options: any) {
add_actions()

function add_actions() {
const actions = prepare_actions(entities)

for (const action of actions) {
switch (action.pattern.cmd) {
case 'load':
seneca.message(action.pattern, make_load(action))
break

case 'save':
seneca.message(action.pattern, make_save(action))
break
}
}
const ents = prepare_ents(entities)

for(const ent of ents) {
seneca.message(ent.load.pattern, (make_load(ent) || unknown_cmd))
seneca.message(ent.save.pattern, (make_save(ent) || unknown_cmd))
}
}

function make_load(action: ActionData) {
return make_actions(action)['load']
function make_load(ent: EntData) {
if(!ent.load.details) return false
return make_actions(ent.load).load
}

function make_save(action: ActionData) {
return make_actions(action)['save']
function make_save(ent: EntData) {
if(!ent.save.details) return false
return make_actions(ent.save).save
}

async function unknown_cmd(this: any, msg: any) {
throw new Error(`undefined action: ${msg.cmd}, entity: ${msg.ent.entity$}`)
}

// prepare links replacing placeholders
function prepare_actions(entities: EntityMap): ActionData[] {
const actions_details: ActionData[] = []
function prepare_ents(entities: EntityMap): EntData[] {
const ents_datas: EntData[] = []

for(const [ent_name, ent_details] of Object.entries(entities)) {
ent_details.name = ent_name
const ent_data: any = { load : {}, save: {} }

for(const [action_name, action_data] of Object.entries(ent_details.actions)) {
const common = {zone:'provider', base:'eventbrite', role:'entity', name: ent_name}

const pattern = {
name: ent_name,
cmd: action_name,
zone: 'provider',
base: 'eventbrite',
role: 'entity',
}
ent_data.load.pattern = {...common, cmd: 'load'}
ent_data.save.pattern = {...common, cmd: 'save'}

actions_details.push({
pattern,
req_fn: prepare_req_fn(action_data.request.method),
...action_data
for(const [action_name, details] of Object.entries(ent_details.actions)) {
Object.assign(ent_data[action_name], {
details,
req_fn: prepare_req_fn(details.request.method)
})
}

ents_datas.push(ent_data)
}
return actions_details

return ents_datas
}

function prepare_req_fn(method: string) {
Expand Down
8 changes: 5 additions & 3 deletions src/make-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { ActionData, Context } from "./types"
import { perform_tasks } from "./utils"

function make_actions(action_data: ActionData) {
const { req_fn, request, after, before } = action_data
const { req_fn, details } = action_data
const { request, after, before } = details
const { path } = request

async function load(this:any, msg:any) {
Expand Down Expand Up @@ -44,14 +45,15 @@ function make_actions(action_data: ActionData) {

const built_path = build_path(path, ent)

if(action_data.request.body) {
body = fill_body(action_data.request.body, ent)
if(action_data.details.request.body) {
body = fill_body(action_data.details.request.body, ent)
}

if(before) {
context = perform_tasks(before, {...context, req: body})
}


const res = await req_fn(built_path, {
body: JSON.stringify(context.req)
})
Expand Down
9 changes: 7 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ type TasksTypesFn = {
del: (task: DelTask, context: Context) => void
}

interface ActionData extends ActionDetails {
type ActionData = {
pattern: Record<string,string>
details: ActionDetails
req_fn: (path:string, options?: Record<any,string>) => Promise<any>
}

export type { EntityMap, EntDetails, ActionData, Task, Context, TasksTypesFn, SetTask, DelTask }
type EntData = {
[key in Actions]: ActionData
}

export type { Actions, EntityMap, EntDetails, ActionData, Task, Context, TasksTypesFn, SetTask, DelTask, EntData }

0 comments on commit 9486308

Please sign in to comment.