All files / src/server/router/prao index.ts

0% Statements 0/22
0% Branches 0/8
0% Functions 0/3
0% Lines 0/21

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80                                                                                                                                                               
import { OWNER_ID } from "@/backendConstants";
import findAll from "@/lib/dbHelpers/findAll";
import { insertOne } from "@/lib/dbHelpers/insertOne";
import { ElevenLabsAccountWithProxyRepositoryPromise } from "@/repository/ElevenLabsAccountWithProxyRepository";
import { PRAOSessionRepositoryPromise } from "@/repository/PRAOSession";
import { tRPCProcedure, tRPCRouter } from "@/server/tRPC";
import { exchangeRefreshTokenForIDToken } from "@/services/elevenLabsFirebase/exchangeRefreshTokenForIDToken";
import { user } from "@/services/elevenLabsInternalAPI/user";
import { getErrorMessage } from "@/utils/getErrorMessage";
import { TRPCError } from "@trpc/server";
 
export const praoRouter = tRPCRouter({
    init: tRPCProcedure.mutation(async () => {
        const ElevenLabsAccountWithProxyRepositoryResult = await ElevenLabsAccountWithProxyRepositoryPromise;
 
        if (ElevenLabsAccountWithProxyRepositoryResult.isErr()) {
            throw new TRPCError({
                code: "INTERNAL_SERVER_ERROR",
                message: "Failed to init 'ElevenLabsAccountWithProxyRepository'",
                cause: ElevenLabsAccountWithProxyRepositoryResult.error,
            });
        }
 
        const ElevenLabsAccountWithProxyRepository = ElevenLabsAccountWithProxyRepositoryResult.value;
 
        const ownerID = OWNER_ID;
        const findAllByOwnerIDResult = await ElevenLabsAccountWithProxyRepository.findAllByOwnerID(ownerID);
 
        if (findAllByOwnerIDResult.isErr()) {
            throw new TRPCError({
                code: "NOT_FOUND",
                message: `Failed to find all elevenLabs accounts for current ownerID`,
            });
        }
 
        const accounts = findAllByOwnerIDResult.value;
        const accountIDs = accounts.map((account) => account._id);
 
        const PRAOSessionRepositoryResult = await PRAOSessionRepositoryPromise;
 
        if (PRAOSessionRepositoryResult.isErr()) {
            throw new TRPCError({
                code: "INTERNAL_SERVER_ERROR",
                message: "Failed to init 'PRAOSessionRepository'",
                cause: PRAOSessionRepositoryResult.error,
            });
        }
 
        const PRAOSessionRepository = PRAOSessionRepositoryResult.value;
 
        const insertOneResult = await PRAOSessionRepository.insertOne({ userID: ownerID, accountIDs });
 
        if (insertOneResult.isErr()) {
            throw new TRPCError({
                code: "INTERNAL_SERVER_ERROR",
                message: getErrorMessage(insertOneResult.error),
                cause: insertOneResult.error,
            });
        }
 
        const sessionID = insertOneResult.value.insertedId;
 
        const idTokenPromises = await Promise.all(
            accounts.map((account) =>
                exchangeRefreshTokenForIDToken({
                    proxyURL: account.proxyURL,
                    refreshToken: account.firebaseAuthCreds.refreshToken,
                }),
            ),
        );
 
        // const isError = idTokenPromises.reduce((idTokenResult => idTokenResult.isErr()), false);
        // const idTokens = idTokenPromises.map(idTokenResult => )
 
        // const userInfoPromises = accounts.map(account => user({proxyURL: account.proxyURL, bearerToken: account.firebaseAuthCreds})
 
        // return;
    }),
});