@bash-app/bash-common 12.1.1 → 12.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/utils/apiUtils.ts +76 -0
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import {BashEvent, Contact, User } from "@prisma/client";
|
|
2
|
+
import {ContactOrUser} from "../definitions";
|
|
3
|
+
import {Public_User} from "../extendedSchemas";
|
|
4
|
+
|
|
5
|
+
type CombinedUser = User | Public_User;
|
|
6
|
+
type CombinedUserOrContact = CombinedUser | Contact;
|
|
7
|
+
|
|
8
|
+
export function isContactAndNotUser(contactOrUser: Partial<ContactOrUser>): contactOrUser is Contact {
|
|
9
|
+
return !!(contactOrUser as Contact).contactOwnerId;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function getUserName(contactOrUser: Partial<CombinedUserOrContact> | undefined): string {
|
|
13
|
+
if (!contactOrUser) {
|
|
14
|
+
return '';
|
|
15
|
+
}
|
|
16
|
+
if (isContactAndNotUser(contactOrUser)) {
|
|
17
|
+
return contactOrUser.fullName ?? (contactOrUser as Contact).contactEmail ?? '';
|
|
18
|
+
}
|
|
19
|
+
const user = contactOrUser as CombinedUser;
|
|
20
|
+
const combinedName = user.givenName && user.familyName ? `${user.givenName} ${user.familyName}` : null;
|
|
21
|
+
return combinedName ?? user.email;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function getUserImage(contactOrUser: CombinedUserOrContact | undefined): string {
|
|
25
|
+
if (!contactOrUser) {
|
|
26
|
+
return '';
|
|
27
|
+
}
|
|
28
|
+
const noProfilePicture = '/noProfilePicture.svg';
|
|
29
|
+
if (isContactAndNotUser(contactOrUser)) {
|
|
30
|
+
return noProfilePicture;
|
|
31
|
+
}
|
|
32
|
+
const user = contactOrUser as CombinedUser;
|
|
33
|
+
return user.uploadedImage ?? user.image ?? noProfilePicture;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function getContactOrUserEmail(contactOrUser: Partial<ContactOrUser>): string {
|
|
37
|
+
return isContactAndNotUser(contactOrUser)
|
|
38
|
+
? (contactOrUser as Contact).contactEmail ?? ''
|
|
39
|
+
: (contactOrUser as User).email;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function getUserIdAndNotContactId(contactOrUser: Partial<ContactOrUser>): string | null {
|
|
43
|
+
return isContactAndNotUser(contactOrUser)
|
|
44
|
+
? null
|
|
45
|
+
: (contactOrUser as User).id;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function getBashEventDetailUrlParamsFromBashEvent(bashEvent: BashEvent | undefined, coverPhoto?: string): string {
|
|
49
|
+
if (!bashEvent) {
|
|
50
|
+
return '';
|
|
51
|
+
}
|
|
52
|
+
return getBashEventDetailUrlParams(bashEvent.title, bashEvent.description, coverPhoto ?? bashEvent.coverPhoto);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function getBashEventDetailUrlParams(bashEventTitle: string | undefined,
|
|
56
|
+
bashEventDescription: string | undefined | null,
|
|
57
|
+
coverPhoto?: string | null): string {
|
|
58
|
+
// Don't let the description be huge
|
|
59
|
+
const desc = bashEventDescription && bashEventDescription.length > 10 ? bashEventDescription?.slice(0, 10) + '...' : bashEventDescription;
|
|
60
|
+
// Need to add characters if any of the args is undefined or else the params get thrown off
|
|
61
|
+
const params: string[] = [];
|
|
62
|
+
if (bashEventTitle) {
|
|
63
|
+
// params.push(`${URL_PARAMS_BASH_EVENT_TITLE}=${encodeURIComponent(bashEventTitle)}`);
|
|
64
|
+
}
|
|
65
|
+
if (desc) {
|
|
66
|
+
// params.push(`${URL_PARAMS_BASH_EVENT_DESC}=${encodeURIComponent(desc)}`);
|
|
67
|
+
}
|
|
68
|
+
if (coverPhoto) {
|
|
69
|
+
// params.push(`${URL_PARAMS_BASH_EVENT_COVER_PHOTO}=${encodeURIComponent(coverPhoto)}`);
|
|
70
|
+
}
|
|
71
|
+
return params.join('&');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function isProduction(): boolean {
|
|
75
|
+
return process.env.NODE_ENV === 'production';
|
|
76
|
+
}
|