-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (112 loc) · 3.42 KB
/
index.js
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
116
117
118
119
const { GraphQLServer } = require("graphql-yoga");
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
const resolvers = {
Query: {
getShareholderById: async (parent, args, context) => {
return await context.prisma.shareholder.findOne({
where: {
id: args.shareholderId,
},
include: {
Movie: true,
BalanceTransaction: {
include: {
Transfer: true,
},
},
},
});
},
getAllMovies: async (parent, args, context) => {
return await context.prisma.movie.findMany();
},
getAllShareholders: async (parent, args, context) => {
return await context.prisma.shareholder.findMany({
include: {
Movie: true,
BalanceTransaction: true,
},
});
},
getAllTransferByMovie: async (parent, args, context) => {
return await context.prisma.transfer.findMany({
where: { Movie: { id: args.movieId } },
});
},
getAllShareholdersByMovie: async (parent, args, context) => {
return await context.prisma.shareholder.findMany({
where: { Movie: { id: args.movieId } },
include: {
BalanceTransaction: true,
},
});
},
},
Mutation: {
createMovie: async (parent, args, context) => {
return await context.prisma.movie.create({
data: { title: args.title, cover: args.cover },
});
},
createShareholder: async (parent, args, context) => {
const shareholder = await context.prisma.shareholder.create({
data: {
firstName: args.firstName,
lastName: args.lastName,
address: args.address,
iban: args.iban,
Movie: {
connect: { id: args.movieId },
},
},
include: { Movie: true, BalanceTransaction: true },
});
return shareholder;
},
createTransfer: async (parent, args, context) => {
/* Step 1: Create transfer */
const transfer = await context.prisma.transfer.create({
data: {
amount: args.amount,
description: args.description,
Movie: {
connect: { id: args.movieId },
},
},
include: { Movie: true },
});
/* Step 2: Find how many shareholders belongs to the film related to the transfer previously created. */
const shareholdersByFilm = await context.prisma.shareholder.findMany({
select: { id: true },
where: { Movie: { id: args.movieId } },
});
/* Step 3: Divide the amount among all the shareholders involved. */
const shareholdersCount = shareholdersByFilm.length;
const balanceQuantity = args.amount / shareholdersCount;
/* Step 4: Create a balance transaction for each shareholder related to the transfer created. */
shareholdersByFilm.map(async (shareholder) => {
await context.prisma.balanceTransaction.create({
data: {
amount: balanceQuantity,
Transfer: {
connect: { id: transfer.id },
},
Shareholder: {
connect: { id: shareholder.id },
},
},
});
});
return transfer;
},
},
};
const server = new GraphQLServer({
typeDefs: "./schema.graphql",
resolvers,
context: {
prisma,
},
});
server.start(() => console.log("server running localhost:4000"));