To implement the Families controller, you need to implement 2 API endpoints, we will use these endpoints to fetch families from your system.
Prerequisites
SDK
import { Integrator } from '@churnkey/sdk'
import { Context } from '../Context'
import { Family } from '../models/Family'
export const Families = Integrator.Families.config({
ctx: Context,
async retrieve(ctx, options) {
const yourFamily = await ctx.db.findFamily(options.id)
return new Family(yourFamily)
},
async list(ctx, options) {
const yourFamilies = await ctx.db.listFamilies({
limit: options.limit,
offset: options.cursor // the value you pass as `next` below
})
return {
data: yourFamilies.map(family => new Family(family)),
// pass the next cursor if there are more items
next: yourFamilies.length === options.limit ? offset + limit : undefined
}
}
})
Endpoints
Retrieve Required
GET /churnkey/families/:id
This endpoint fetches Family by its id. Usually, implementation will include finding a family in your database and mapping it to the Family model.
Must return Family model. See Family model documentation.
See Error Responses.
import { Family } from '../models/Family'
app.get('/churnkey/families/:id', async (req, res) => {
const family = await db.findFamilyById(req.params.id)
if (!family) {
return res.status(404).send({ code: 404, message: 'Family not found' })
}
res.send(new Family(family))
})
List Required
GET /churnkey/families
This endpoint fetches a list of families from your database. You should find families in your database (with pagination), map them to the Family model and return a paginated list.
Maximum number of items to return
Cursor for pagination. The actual value is whatever you decided to use as next in the response.
Array of items. The type of item is defined in the endpoint documentation, e.g. Customer for /customers endpoint
Either a next id or an offset for the next page. You decide what to use, we will send next value as a cursor query parameter back to you. If next is empty, there are no more pages
import { Family } from '../models/Family'
app.get('/churnkey/families', async (req, res) => {
const limit = Number.parseInt(req.query.limit)
const offset = Number.parseInt(req.query.cursor)
const families = await db.findFamilies({ limit, offset })
res.send({
data: families.map(c => new Family(c)),
next: families.length === limit ? offset + limit : undefined
})
})
Webhooks
Coming soon.