/
fixtures.ts
99 lines (87 loc) · 2.37 KB
/
fixtures.ts
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
import { faker } from "@faker-js/faker";
import { prisma } from "../db";
import {
Bottle as BottleType,
Brand as BrandType,
Checkin as CheckinType,
Distiller as DistillerType,
Prisma,
User as UserType,
} from "@prisma/client";
import { createAccessToken } from "../auth";
function between(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1) + min);
}
export const User = async ({ ...data }: Partial<UserType> = {}) => {
return await prisma.user.create({
data: {
displayName: faker.name.firstName(),
email: faker.internet.email(),
admin: false,
active: true,
...data,
},
});
};
export const Brand = async ({ ...data }: Partial<BrandType> = {}) => {
return await prisma.brand.create({
data: {
name: faker.company.name(),
country: faker.address.country(),
public: true,
...data,
},
});
};
export const Distiller = async ({ ...data }: Partial<DistillerType> = {}) => {
return await prisma.distiller.create({
data: {
name: faker.company.name(),
country: faker.address.country(),
public: true,
...data,
},
});
};
export const Bottle = async ({ distillerIds = [], ...data }: any = {}) => {
if (data.brandId === undefined) data.brandId = (await Brand()).id;
const distillers: Prisma.DistillerCreateNestedManyWithoutBottlesInput = {};
if (!distillerIds.length) {
if (between(0, 1) === 1) {
distillers.connect = [{ id: (await Distiller()).id }];
}
} else {
distillers.connect = distillerIds.map((d: string) => ({ id: d }));
}
return await prisma.bottle.create({
data: {
name: faker.music.songName(),
...data,
distillers,
},
});
};
export const Checkin = async ({ ...data }: any = {}) => {
if (!data.bottleId) data.bottleId = (await Bottle()).id;
if (!data.userId) data.userId = (await User()).id;
return await prisma.checkin.create({
data: {
tastingNotes: faker.lorem.sentence(),
rating: faker.datatype.float({ min: 1, max: 5 }),
...data,
},
});
};
export const AuthToken = async ({ user }: { user?: UserType | null } = {}) => {
if (!user) user = await User();
return createAccessToken(user);
};
export const AuthenticatedHeaders = async ({
user,
}: {
user?: UserType | null;
} = {}) => {
return {
Authorization: `Bearer ${await AuthToken({ user })}`,
};
};