import { Subscription } from "../models" ; import { NextFunction , Request , Response } from "express" ; export default async ( req , res , next ) : Promise < void > => { const { github , client , isAdmin , githubToken } = res . locals ; if ( ! githubToken ) { return next ( new Error ( "Unauthorized" ) ) ; } const installationId = Number ( req . params . installationId ) ; try { const { data : { login } } = await github . users . getAuthenticated ( ) ; const { data : installation } = await client . apps . getInstallation ( { installation_id : installationId } ) ; const subscriptions = await Subscription . getAllForInstallation ( installationId ) ; if ( await isAdmin ( { org : installation . account . login , username : login , type : installation . target_type } ) ) { const { data : info } = await client . apps . getAuthenticated ( ) ; return res . render ( "github-subscriptions.hbs" , { csrfToken : req . csrfToken ( ) , nonce : res . locals . nonce , installation , info , host : res . locals . jiraHost , subscriptions , hasSubscriptions : subscriptions . length > 0 } ) ; } else { return next ( new Error ( "Unauthorized" ) ) ; } } catch ( err ) { req . log . error ( err , "Unable to show subscription page" ) ; return next ( new Error ( "Not Found" ) ) ; } } ;