All files / src/gmail/providers gmailTransactionSourceProvider.ts

100% Statements 20/20
50% Branches 2/4
100% Functions 6/6
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 558x         8x 8x   8x     8x               5x 5x       5x       3x       23x   22x   22x       23x   23x   22x       22x   22x   22x    
import { inject, injectable } from "inversify";
import GmailMessageData from "../types/gmailMessageData";
import ITransactionSourceProvider from "../../core/contracts/ITransactionSourceProvider";
import IUsesGoogleOAuth2 from "../../googleOAuth2/contracts/IUsesGoogleOAuth2";
import GmailApiClient from "../clients/gmailApiClient";
import { injectables } from "../../core/types/injectables";
import ILogger from "../../core/contracts/ILogger";
import GoogleOAuth2Identifiers from "../../googleOAuth2/types/googleOAuth2Identifiers";
import { DependencyInjector } from "../../dependencyInjector";
 
@injectable()
export default class GmailTransactionSourceProvider implements ITransactionSourceProvider, IUsesGoogleOAuth2 {
    private readonly logger;
    private gmailApiClient: GmailApiClient;
 
    public constructor(
        @inject(injectables.ILogger)
        logger: ILogger
    ) {
        this.logger = logger;
        this.gmailApiClient = null!;
    }
 
    public async useOAuth2IdentifiersAsync(identifiers: GoogleOAuth2Identifiers) {
        this.gmailApiClient = await DependencyInjector.Singleton.generateGmailServiceAsync(injectables.GmailApiClientGenerator, identifiers);
    }
 
    public generateTransactionIdsAsync() {
        return this.gmailApiClient.generateMessageIdsAsync();
    }
    
    public async getAsync(transactionId: string) {
        const messageData = await this.getGmailMessageDataAsync(transactionId);
 
        const attachmentData = await this.getAttachmentDataAsync(messageData);
        
        return attachmentData;
    }
 
    private async getGmailMessageDataAsync(transactionId: string) {
        this.logger.log(`Fetching Gmail message...`, { transactionId: transactionId });
 
        const gmailMessageData = await this.gmailApiClient.fetchMessageDataAsync(transactionId);
 
        return gmailMessageData;
    }
 
    private async getAttachmentDataAsync(messageData: GmailMessageData) {
        this.logger.log(`Fetching Gmail attachment...`, { transactionId: messageData.messageId });
 
        const attachmentData = await this.gmailApiClient.fetchAttachmentDataAsync(messageData);
 
        return attachmentData;
    }
}