GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — partypocket/lib/Party.ts at main · schiemon/partypocket

This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Expand file tree

/

Party.ts

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
156 lines (134 loc) · 4.21 KB
/

Party.ts

Copy path

File metadata and controls

156 lines (134 loc) · 4.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { Party } from "@prisma/client";
import prisma, { TransactionablePrisma } from "../lib/db";
import {
PARTY_DESCRIPTION_MAX_LEN,
PARTY_MAX_COST_PER_GUEST,
PARTY_NAME_MAX_LEN,
PARTY_NAME_MIN_LEN,
} from "./constants";
import { StatusCodes } from "http-status-codes";
import ApiError from "./error/ApiError";
import RangeError from "./error/RangeError";
export type PartyCreationData = Omit<Party, "id" | "host_id">;
class PartyError extends ApiError {
constructor(status: StatusCodes, message: string) {
super(status, message);
}
}
export class PartyNotFoundError extends PartyError {
constructor(partyId: string, message?: string) {
if (!message) message = `Party with id ${partyId} does not exist.`;
super(StatusCodes.BAD_REQUEST, message);
}
}
export class PartyDuplicateError extends PartyError {
constructor(partyName: string, host_id: string, message?: string) {
if (!message)
message = `Party with name ${partyName} already exist at host ${host_id}.`;
super(StatusCodes.BAD_REQUEST, message);
}
}
export async function createParty(
hostId: string,
partyData: PartyCreationData
): Promise<Party> {
const { name, description, cost_per_guest } = partyData;
if (!(PARTY_NAME_MIN_LEN <= name.length && name.length <= PARTY_NAME_MAX_LEN))
throw RangeError.fromRangeViolation(
"Length of a party's name",
PARTY_NAME_MIN_LEN,
PARTY_NAME_MAX_LEN
);
if (description && description.length > PARTY_DESCRIPTION_MAX_LEN)
throw RangeError.fromRangeViolation(
"Length of a party's description",
0,
PARTY_DESCRIPTION_MAX_LEN
);
if (cost_per_guest <= 0)
throw RangeError.fromPositivityViolation("Cost per guest");
if (cost_per_guest > PARTY_MAX_COST_PER_GUEST)
throw RangeError.fromUpperLimitViolation(
"Cost per guest",
PARTY_MAX_COST_PER_GUEST
);
return prisma.$transaction<Party>(async (prismaClient) => {
// We first have to check whether the party name at the host already given.
// With "at the host" mean the belonging of the party to the host.
const duplicate = await prismaClient.party.findUnique({
where: {
uniq_party_name_host_id: {
name: partyData.name,
host_id: hostId,
},
},
});
if (duplicate !== null)
throw new PartyDuplicateError(partyData.name, hostId);
// Then we can add the guest to the party.
return prismaClient.party.create({
data: { ...partyData, host_id: hostId },
});
});
}
export async function getPartiesOf(hostId: string): Promise<Party[]> {
return prisma.party.findMany({
where: {
host: {
id: hostId,
},
},
});
}
export async function getParty(partyId: string): Promise<Party | null> {
return prisma.party.findUnique({
where: {
id: partyId,
},
});
}
export async function deleteParty(partyId: string): Promise<Party> {
const deleteGuests = prisma.guest.deleteMany({
where: {
party_id: partyId,
},
});
const deleteActualParty = prisma.party.delete({
where: {
id: partyId,
},
});
return prisma
.$transaction([deleteGuests, deleteActualParty])
.then(([_, party]) => party);
}
export async function existsPartyById(
partyId: string,
prismaClient?: TransactionablePrisma
): Promise<boolean> {
if (!prismaClient) prismaClient = prisma;
return (
(await prismaClient.party.count({
where: {
id: partyId,
},
})) === 1
);
}
export async function existsPartyByName(
partyName: string,
host_id: string,
prismaClient?: TransactionablePrisma
): Promise<boolean> {
if (!prismaClient) prismaClient = prisma;
return (
(await prismaClient.party.count({
where: {
name: partyName,
host: {
id: host_id,
},
},
})) === 1
);
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.