@prisma/client#UseCase TypeScript Examples

The following examples show how to use @prisma/client#UseCase. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: auth.ts    From frames with Mozilla Public License 2.0 6 votes vote down vote up
/**
     * @desc clears out the auth key by assigning a user to it and updating the accessing
     * @param authKey - auth key to be cleared
     * @param userId - user id to be assigned
     * @param useCase - use case of the request
     * @param authView - auth view of the request
     */
    public async utiliseAuthKey(authKey: string, userId: string, useCase: UseCase, authView: string | null = null) {
        const auth = await this.prisma.auth.findUnique({where: {authKey}});
        const user = await this.prisma.user.findUnique({where: {userId}});
        if (user && auth && auth.access === 0)
            await this.prisma.auth.update({
                where: {authKey},
                data: {userId, access: auth.access + 1, auth: authView, useCase}
            })

        else if (authKey !== 'homeBase')
            throw new Error('Unauthorised access attempted');
    }
Example #2
Source File: playback.ts    From frames with Mozilla Public License 2.0 6 votes vote down vote up
/**
     * @desc adds a file to the download queue if the auth token is valid and the auth file exists
     * @param auth - the auth location of the file
     * @param authKey - the auth token to be validated
     * @param userId - the user's identifier
     */
    public async addFileToDownload(auth: string, authKey: string, userId: string): Promise<string | null> {
        const user = new User();
        const file = await this.prisma.view.findFirst({where: {auth}, select: {video: true}});
        const userFile = await this.prisma.user.findUnique({where: {userId}});
        const valid = await user.validateAuthKey(authKey, userFile?.role || Role.USER);
        if (valid === 0 && file && userFile) {
            await user.utiliseAuthKey(authKey, userId, UseCase.DOWNLOAD, auth);
            const location = this.createUUID();
            await this.prisma.download.create({
                data: {
                    location, auth, userId
                }
            });
            return location;
        }

        return null;
    }
Example #3
Source File: manageKeys.tsx    From frames with Mozilla Public License 2.0 5 votes vote down vote up
function KeyHolder({obj}: { obj: ManageAuthKey }) {
    const dispatch = useSetRecoilState(InformDisplayContext);

    const copy = async () => {
        if (obj.access === 0)
            navigator.clipboard.writeText(obj.key)
                .then(() => {
                    dispatch({
                        type: "alert",
                        heading: 'Copy Successful',
                        message: 'auth key copied successfully'
                    })
                })
                .catch((error) => {
                    dispatch({
                        type: "error",
                        heading: 'Something went wrong',
                        message: error as string
                    })
                })

        else dispatch({
            type: "warn",
            heading: 'Invalid action',
            message: 'This auth key has been exhausted'
        })
    }

    return (
        <div className={obj.access !== 0 ? `${ss.res} ${ss.had}` : ss.res} onClick={copy}>
            <img src={obj.backdrop} alt={obj.name}
                 className={obj.case === UseCase.SIGNUP ? ss.resPerson : ss.resImage}/>
            <div className={ss.resDiv}>
                <div className={ss.resSpan}>
                    <span>{obj.name}</span>
                </div>
                <p className="overview">{obj.description}</p>
            </div>
        </div>
    )
}
Example #4
Source File: auth.ts    From frames with Mozilla Public License 2.0 5 votes vote down vote up
/**
     * @desc provides information about all keys on the database
     * @param userId - user requesting the information
     */
    public async getKeys(userId: string): Promise<ManageAuthKey[]> {
        if (await this.validateUser(userId)) {
            const keys = await this.prisma.auth.findMany({
                include: {
                    user: true,
                    view: {include: {episode: true, video: {include: {media: true}}}}
                }, orderBy: {id: 'desc'}
            });

            const response: ManageAuthKey[] = [];

            for (let item of keys) {
                let description = '';
                let backdrop = '';

                if (item.access === 0)
                    description = item.user.email + ' created this auth key';

                else {
                    description = item.useCase === UseCase.SIGNUP ? item.user.email + ' signed up with this auth key' : '';

                    if (item.view) {
                        let media = item.view.video.media.name;
                        backdrop = item.view.video.media.backdrop;

                        if (item.view.episode)
                            media = media + `: S${item.view.episode.seasonId}, E${item.view.episode.episode}`;

                        description = item.user.email + ' downloaded ' + media + ' using this auth key';
                    }
                }

                response.push({
                    case: item.useCase,
                    description, backdrop, key: item.authKey,
                    name: 'Key: ' + item.authKey, access: item.access
                })
            }

            return response;
        }

        return []
    }
Example #5
Source File: auth.ts    From frames with Mozilla Public License 2.0 5 votes vote down vote up
/**
     * @desc creates a new user with the given details
     * @param email - email of the user
     * @param password - password of the user
     * @param authKey - auth key of the user
     * @param role - role of the user
     * @returns Promise<AuthInterface> auth object on with either error or response on success
     */
    public async register(email: string, password: string, authKey: string, role?: Role): Promise<AuthInterface> {
        role = role || Role.USER;
        const confirmedEmail = role === Role.OAUTH || role === Role.GUEST;
        password = await bcrypt.hash(password, 10);
        const notificationChannel = this.generateKey(13, 7);
        let userId = this.createUUID();

        let user = await this.prisma.user.findFirst({where:{email}});

        if (user)
            return {error: 'this email already exists'};

        const validAuth = await this.validateAuthKey(authKey, role);
        if (validAuth !== 0) {
            const error = validAuth === -1 ? 'invalid auth key' : 'this auth key has already been used';
            return {error};
        }

        const userRes = await this.prisma.user.create({
            data: {email, password, userId, role, confirmedEmail, notificationChannel}
        });

        await this.utiliseAuthKey(authKey, userId, UseCase.SIGNUP);
        return !confirmedEmail ? {error: 'Please check your email for a verification link'}: {
            response: 'User created successfully',
            payLoad: {
                email: userRes.email,
                context: userRes.role,
                notificationChannel: userRes.notificationChannel,
                ...await this.generateSession(userRes.userId)
            }
        };
    }