import { RepositoryModel } from '../models/index.model' ; import { DocumentData , DocumentReference , DocumentSnapshot , FirebaseAdmin , Query , QuerySnapshot , Transaction } from './../client/firebase-admin' ; import { GitHubClientDelete } from './../client/github' ; import { Logger } from './../client/logger' ; export interface DeleteGitWebhookRepositoryInput { token : string ; data : { uid ? : string ; id ? : number ; } ; } export const onDeleteGitWebhookRepository = async ( token , data ) => { if ( ! ( data && ( data . uid || data . id ) ) ) { Logger . error ( 'Invalid input data!' ) ; return null ; } if ( data . uid ) { const repositoryRef = RepositoryModel . getRepositoryReference ( data . uid ) ; await FirebaseAdmin . firestore ( ) . runTransaction ( ( t ) => { return t . get ( repositoryRef ) . then ( ( snap ) => { const repository = snap . data ( ) ; if ( repository && repository . webhook ) { return deleteWebhook ( repository . fullName , repository . webhook . id , token ) . then ( ( ) => { repository . webhook = null ; Logger . info ( `Webhook removed for ${ repository . fullName }` ) ; return repositoryRef . update ( repository ) ; } ) . catch ( ( error ) => { Logger . info ( `No Webhook for ${ repository . fullName }` ) ; Logger . error ( error ) ; return repository ; } ) ; } Logger . info ( `No found repository (uid = ${ data . uid }) or webhook` ) ; return null ; } ) ; } ) ; } else if ( data . id ) { const query = RepositoryModel . getRepositoryReferenceById ( data . id ) ; await FirebaseAdmin . firestore ( ) . runTransaction ( ( t ) => { return t . get ( query ) . then ( ( snap ) => { const repositoryRef = snap . docs . shift ( ) . ref ; const repository = snap . docs . shift ( ) . data ( ) ; if ( repository && repository . webhook ) { return deleteWebhook ( repository . fullName , repository . webhook . id , token ) . then ( ( ) => { repository . webhook = null ; Logger . info ( `Webhook removed for ${ repository . fullName }` ) ; return repositoryRef . update ( repository ) ; } ) . catch ( ( error ) => { Logger . info ( `No Webhook for ${ repository . fullName }` ) ; Logger . error ( error ) ; return repositoryRef ; } ) ; } Logger . info ( `No found repository (id = ${ data . id }) or webhook` ) ; return null ; } ) ; } ) ; } } ; export function deleteWebhook ( repositoryFullName , webhookId , token ) { return GitHubClientDelete < void > ( `/repos/${ repositoryFullName }/hooks/${ webhookId }` , token ) ; }