Controllers

Coupons - Controller

List and retrieve `Coupon` models.

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

Prerequisites

Coupon Model

A code that can be redeemed for a discount.

Required

SDK

If you are using the SDK, you can implement the Coupons 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 { Coupon } from '../models/Coupon'

export const Coupons = Integrator.Coupons.config({
    ctx: Context,
    async retrieve(ctx, options) {
        const yourCoupon = await ctx.db.findCoupon(options.id)
        return new Coupon(yourCoupon)
    },
    async list(ctx, options) {
        const yourCoupons = await ctx.db.listCoupons({
            limit: options.limit,
            offset: options.cursor // the value you pass as `next` below
        })
        return {
            data: yourCoupons.map(coupon => new Coupon(coupon)),
            // pass the next cursor if there are more items
            next: yourCoupons.length === options.limit ? offset + limit : undefined
        }
    }
})

Endpoints

Retrieve Required

GET /churnkey/coupons/:id

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

List Required

GET /churnkey/coupons

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

Learn more about pagination.

Webhooks

Coming soon.