eletrotupi / tcc/ api/src/services/user.service.ts05053ef0b79244f7b58a8aa1d3b1c7ca02787c23
1.4 KB | 69 lines Raw
1
import { prisma } from '@app/lib/prisma';
2
import { User } from '@prisma/client';
3
import bcrypt from 'bcryptjs';
4

5
import {
6
  CreateUserInput,
7
  UpdateUserInput
8
} from "@app/schemas";
9

10
export const createUser = async(input: CreateUserInput): Promise<User> => {
11
  const { password, ...data } = input;
12
  const encryptedPassword = bcrypt.hashSync(password, 10);
13

14
  return await prisma.user.create({
15
    data: {
16
      ...data, encryptedPassword
17
    }
18
  })
19
};
20

21
export const findUserById = async(id: number): Promise<User | null> => {
22
  return await prisma.user.findUnique({
23
    where: {
24
      id
25
    }
26
  });
27
};
28

29
export const findUserByEmail = async(email: string): Promise<User | null> => {
30
  return await prisma.user.findUnique({
31
    where: {
32
      email
33
    }
34
  });
35
};
36

37
export const findUserByActivationCode = async(code: string): Promise<User | null> => {
38
  return await prisma.user.findFirst({
39
    where: {
40
      activationCode: code
41
    }
42
  });
43
}
44

45
export const updateUser = async (input: UpdateUserInput): Promise<User> => {
46
  const { password, ...data } = input;
47

48
  const updateData = {
49
    ...data,
50
    ...(password && { encryptedPassword: bcrypt.hashSync(password, 10) })
51
  };
52

53
  return await prisma.user.update({
54
    where: {
55
      id: input.id
56
    },
57
    data: updateData
58
  })
59
}
60

61
export const findUsersElligibleForPush = async (): Promise<User[]> => {
62
  return await prisma.user.findMany({
63
    where: {
64
      pushToken: {
65
        not: null
66
      }
67
    }
68
  });
69
}