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

0% Statements 0/30
0% Branches 0/16
0% Functions 0/2
0% Lines 0/30

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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115                                                                                                                                                                                                                                     
import { OWNER_ID } from "@/backendConstants";
import { createProxyURL } from "@/brands/proxyURL";
import { ElevenLabsAccountWithProxyRepositoryPromise } from "@/repository/ElevenLabsAccountWithProxyRepository";
import { ElevenLabsAccountWithProxySchema } from "@/schemas/ElevenLabsAccountWithProxy";
import { tRPCProcedure, tRPCRouter } from "@/server/tRPC";
import { signInWithPassword } from "@/services/elevenLabsFirebase/signInWithPassword";
import { getErrorMessage } from "@/utils/getErrorMessage";
import { TRPCError } from "@trpc/server";
import { omit } from "lodash";
import { MongoError } from "mongodb";
 
export const elevenLabsAccountWithProxyRouter = tRPCRouter({
    add: tRPCProcedure.input(ElevenLabsAccountWithProxySchema).mutation(async ({ input }) => {
        const ElevenLabsAccountWithProxyRepositoryResult = await ElevenLabsAccountWithProxyRepositoryPromise;
 
        if (ElevenLabsAccountWithProxyRepositoryResult.isErr()) {
            throw new TRPCError({
                code: "INTERNAL_SERVER_ERROR",
                message: "Failed to init 'ElevenLabsAccountWithProxyRepository'",
                cause: ElevenLabsAccountWithProxyRepositoryResult.error.cause,
            });
        }
 
        const ElevenLabsAccountWithProxyRepository = ElevenLabsAccountWithProxyRepositoryResult.value;
        const proxyURLResult = createProxyURL(
            input.proxy.username,
            input.proxy.password,
            input.proxy.host,
            input.proxy.port,
        );
 
        if (proxyURLResult.isErr()) {
            throw new TRPCError({
                code: "BAD_REQUEST",
                message: `Failed to create proxyURL from username: '${input.proxy.username}', password: '${input.proxy.password}, host: '${input.proxy.host}', port: '${input.proxy.port}'`,
            });
        }
 
        const proxyURL = proxyURLResult.value;
 
        const elevenLabsFirebaseSignInResult = await signInWithPassword({
            email: input.email,
            password: input.password,
            proxyURL,
        });
 
        if (elevenLabsFirebaseSignInResult.isErr()) {
            throw new TRPCError({
                code: "INTERNAL_SERVER_ERROR",
                message: getErrorMessage(elevenLabsFirebaseSignInResult.error),
                cause: elevenLabsFirebaseSignInResult.error,
            });
        }
 
        const refreshToken = elevenLabsFirebaseSignInResult.value.refreshToken;
 
        const ownerID = OWNER_ID;
        const elevenLabsUserID = elevenLabsFirebaseSignInResult.value.localID;
 
        const insertOneResult = await ElevenLabsAccountWithProxyRepository.insertOne({
            ownerID,
            _id: elevenLabsUserID,
            ...omit(input, "proxy"),
            proxyURL,
            firebaseAuthCreds: {
                refreshToken,
            },
        });
 
        if (insertOneResult.isErr()) {
            const error = insertOneResult.error;
 
            console.log("error:", error);
            if (error instanceof MongoError && error.code === 11000) {
                throw new TRPCError({
                    code: "BAD_REQUEST",
                    message: `ElevenLabs userID '${elevenLabsUserID}' already present`,
                    cause: error.cause,
                });
            }
 
            throw new TRPCError({
                code: "INTERNAL_SERVER_ERROR",
                message: "Failed to insert 'elevenLabsAccountWithProxy'",
                cause: error.cause,
            });
        }
 
        return insertOneResult.value.insertedId;
    }),
 
    get: tRPCProcedure.query(async () => {
        const ElevenLabsAccountWithProxyRepositoryResult = await ElevenLabsAccountWithProxyRepositoryPromise;
 
        if (ElevenLabsAccountWithProxyRepositoryResult.isErr()) {
            throw new TRPCError({
                code: "INTERNAL_SERVER_ERROR",
                message: "Failed to init 'ElevenLabsAccountWithProxyRepository'",
                cause: ElevenLabsAccountWithProxyRepositoryResult.error.cause,
            });
        }
 
        const ElevenLabsAccountWithProxyRepository = ElevenLabsAccountWithProxyRepositoryResult.value;
 
        const findAllResult = await ElevenLabsAccountWithProxyRepository.findAllSummaries();
 
        if (findAllResult.isErr()) {
            throw new TRPCError({
                code: "INTERNAL_SERVER_ERROR",
                message: "Error finding all 'elevenLabsAccountsWithProxy",
            });
        }
    }),
});