Controllers

Prices - Controller

List and retrieve `Price` models.

To implement the Prices controller, you need to implement 2 API endpoints, we will use these endpoints to fetch prices from your system.

Prerequisites

Price Model

A price for a product or service.

Required

Products - Controller

If you chose to implement prices with products, you must implement a Products controller first.

Optional

SDK

If you are using the SDK, you can implement the Prices controller by following the code example below. You don't need to get into the details of the API endpoints, the SDK will take care of that for you.

import { Integrator } from '@churnkey/sdk'
import { Context } from '../Context'
import { Price } from '../models/Price'
// import { Products } from './Products'

export const Prices = Integrator.Prices.config({
    ctx: Context,
    type: Integrator.Price.Type.Standalone,
    // type: Integrator.Price.Type.Product,
    // Products: Products, 
    async retrieve(ctx, options) {
        const yourPrice = await ctx.db.findPrice(options.id)
        return new Price(yourPrice)
    },
    async list(ctx, options) {
        const yourPrices = await ctx.db.listPrices({
            limit: options.limit,
            offset: options.cursor // the value you pass as `next` below
        })
        return {
            data: yourPrices.map(price => new Price(price)),
            // pass the next cursor if there are more items
            next: yourPrices.length === options.limit ? offset + limit : undefined
        }
    }
})

Endpoints

Retrieve Required

GET /churnkey/prices/:id

This endpoint fetches Price by its id. Usually, implementation will include finding a price in your database and mapping it to the Price model.

List Required

GET /churnkey/prices

This endpoint fetches a list of prices from your database. You should find prices in your database (with pagination), map them to the Price model and return a paginated list.

Learn more about pagination.