Skip to content

Instantly share code, notes, and snippets.

@mlevkovsky
Created January 7, 2022 22:36
Show Gist options
  • Save mlevkovsky/5bc55e11780fe01fe2b424c3b2cd1145 to your computer and use it in GitHub Desktop.
Save mlevkovsky/5bc55e11780fe01fe2b424c3b2cd1145 to your computer and use it in GitHub Desktop.
example controller
import { inject, injectable } from 'inversify';
import { Controller, Get, Patch } from 'inversify-restify-utils';
import { Next, Request, Response } from 'restify';
import { Merchant } from '../entities/Merchant';
import authenticate from '../helpers/JwtAuthenticator';
import { MerchantService } from '../services/MerchantService';
@injectable()
@Controller('/merchant')
export class MerchantController {
constructor(
@inject('MerchantService') protected merchantService: MerchantService,
@inject('Logger') protected logger: any,
) {}
public static cleanMerchantResult(merchant: Merchant): Merchant {
if (merchant.applicationCharge) {
merchant.applicationCharge.merchant = null;
}
if (merchant.auth) {
merchant.auth = null;
}
return merchant;
}
@Get('/')
public async merchant(req: Request, res: Response, next: Next) {
if (!authenticate(req)) {
res.json(401, {message: 'Not Authorized'});
next();
}
try {
const merchant = await this.merchantService.getOne(req.query, true, true);
res.json(200, {merchant: MerchantController.cleanMerchantResult(merchant)});
} catch (e) {
this.logger.error(`getting merchant error ${e}`,
{meta: {controller: 'MerchantController', error: e, stackTrace: e.stack, merchant: req.query.shop}});
res.json(500, { error: e.body });
}
return next();
}
@Patch('/onboardingComplete')
public async completeOnboarding(req: Request, res: Response, next: Next) {
try {
const merchant = await this.merchantService.completeOnboarding(req.query);
res.json(200, {merchant: MerchantController.cleanMerchantResult(merchant)});
} catch (e) {
this.logger.error(`onboarding complete error merchant error ${e}`,
{meta: {controller: 'MerchantController', error: e, stackTrace: e.stack, merchant: req.query.shop}});
res.json(500, { error: e.body });
}
return next();
}
@Patch('/disableDummyMode')
public async disableDummyMode(req: Request, res: Response, next: Next) {
try {
const merchant = await this.merchantService.disableDummyMode(req.query);
res.json(200, {merchant: MerchantController.cleanMerchantResult(merchant)});
} catch (e) {
this.logger.error(`disabling dummy mode error merchant error ${e}`,
{meta: {controller: 'MerchantController', error: e, stackTrace: e.stack, merchant: req.query.shop}});
res.json(500, { error: e.body });
}
return next();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment