@bash-app/bash-common 1.14.0 → 2.1.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bash-app/bash-common",
3
- "version": "1.14.0",
3
+ "version": "2.1.0",
4
4
  "description": "Common data and scripts to use on the frontend and backend",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -157,6 +157,7 @@ export interface StripeCreateBashEventTicketsCheckoutSessionArgs {
157
157
  currency: string;
158
158
  paymentMethodType: string;
159
159
  ticketListStr: string;
160
+ promoCode?: string;
160
161
  }
161
162
 
162
163
  export interface StripeCreateBashEventDonationCheckoutSessionArgs {
@@ -1,5 +1,5 @@
1
1
  import {DateType, DateValueType} from "react-tailwindcss-datepicker";
2
- import dayjs from "dayjs";
2
+ import dayjs, {Dayjs} from "dayjs";
3
3
  import dayjsUtc from "dayjs/plugin/utc";
4
4
  import dayjsTimeZone from "dayjs/plugin/timezone";
5
5
  import {DateTimeArgType} from "../definitions";
@@ -141,14 +141,13 @@ export function setTimeOnDate(date: DateType | Date | undefined, parsedTime: ITi
141
141
  return correctTimeOnDate;
142
142
  }
143
143
 
144
- export function ensureDateTimeIsLocalDateTime(dateArg: DateType | undefined): Date {
144
+ export function ensureDateTimeIsLocalDateTime(dateArg: DateType | Dayjs | undefined): Dayjs {
145
145
  if (dayjs(dateArg).isUTC()) {
146
146
  const dateLocalTimeZone = dayjs(dateArg);
147
- const date = dateLocalTimeZone.toDate();
148
- return date;
147
+ return dateLocalTimeZone;
149
148
  }
150
149
  else {
151
- return new Date(dateArg ?? Date.now());
150
+ return dayjs(dateArg ?? Date.now());
152
151
  }
153
152
  }
154
153
 
@@ -1,7 +1,27 @@
1
- import {TicketTier} from "@prisma/client";
1
+ import {BashEventPromoCode, TicketTier} from "@prisma/client";
2
2
  import {BASH_FEE_PERCENTAGE, NumberOfTicketsForDate, PRICE_DOLLARS_AND_CENTS_RATIO} from "../definitions";
3
3
 
4
4
 
5
+ /**
6
+ * Returns the amount of discount in dollars
7
+ * @param totalInDollars
8
+ * @param promoCode
9
+ */
10
+ export function calculateDiscountFromPromoCode(totalInDollars: number, promoCode: BashEventPromoCode | undefined): number {
11
+ if (promoCode) {
12
+ if (promoCode.discountAmountInCents) {
13
+ const discountInDollars = convertCentsToDollars(promoCode.discountAmountInCents);
14
+ return Math.max(0, discountInDollars); // Ensure we don't discount more than 100%
15
+ }
16
+ if (promoCode.discountAmountPercentage) {
17
+ const discountInDollars = totalInDollars * promoCode.discountAmountPercentage;
18
+ return Math.min(totalInDollars, discountInDollars); // Ensure we don't discount more than 100%
19
+ }
20
+ }
21
+ return 0;
22
+ }
23
+
24
+
5
25
  /**
6
26
  * Returns the total price based on a map where the keys are the ticketTierIds
7
27
  * @param ticketTiers
@@ -18,13 +38,13 @@ export function calculateTotalPriceWithoutTax(ticketTiers: TicketTier[], ticketL
18
38
  return total;
19
39
  }
20
40
 
21
- export function convertCentsToDollars(price: number | undefined): number {
41
+ export function convertCentsToDollars(price: number | undefined | null): number {
22
42
  if (!price) {
23
43
  return 0;
24
44
  }
25
45
  return price / PRICE_DOLLARS_AND_CENTS_RATIO;
26
46
  }
27
- export function convertDollarsToCents(price: number | undefined): number {
47
+ export function convertDollarsToCents(price: number | undefined | null): number {
28
48
  if (!price) {
29
49
  return 0;
30
50
  }