Implementation
First step is to declare the smart collection user_stripe_cards in auser-stripe-cards.js file in the forest folder.
forest/users.js file.
routes/users.js file.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
user-stripe-cards.js file in the forest folder.
const { collection } = require('forest-express-sequelize');
const models = require('../models');
collection('users_stripe_cards', {
isSearchable: true,
fields: [
{
field: 'id',
type: 'String',
},
{
field: 'country',
type: 'String',
},
{
field: 'brand',
type: 'String',
},
{
field: 'exp_month',
type: 'Number',
},
{
field: 'exp_year',
type: 'Number',
},
{
field: 'last4',
type: 'Number',
},
],
});
forest/users.js file.
collection('users', {
actions: [],
fields: [
{
field: 'stripe-cards',
type: ['String'],
reference: 'users_stripe_cards.id',
},
],
segments: [],
});
routes/users.js file.
const express = require('express');
const { PermissionMiddlewareCreator } = require('forest-express-sequelize');
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const { users, usersData } = require('../models');
var JSONAPISerializer = require('jsonapi-serializer').Serializer;
const router = express.Router();
const permissionMiddlewareCreator = new PermissionMiddlewareCreator('users');
router.get(
'/users/:userId/relationships/stripe-cards',
async (request, response, next) => {
const UserStripeCardSerializer = new JSONAPISerializer(
'users_stripe_cards',
{
attributes: ['country', 'brand', 'exp_year', 'exp_month', 'last4'],
}
);
const { userId } = request.params;
const stripeId = await usersData
.findOne({
where: { userId },
})
.then((userData) => userData.stripeId)
.catch(() => null);
const cardsInfo = await stripe.customers.listSources(stripeId, {
object: 'card',
limit: 3,
});
const data = UserStripeCardSerializer.serialize(cardsInfo.data);
return response.send(data);
}
);
module.exports = router;
Was this page helpful?