tripstorekrabi-shared 1.8.0 → 1.9.1

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.
Files changed (2) hide show
  1. package/booking.js +118 -0
  2. package/package.json +5 -4
package/booking.js ADDED
@@ -0,0 +1,118 @@
1
+ import dayjs from "dayjs";
2
+ import { calculateTotalPrice } from './price.js';
3
+
4
+ export function addBookingFromInvoice(firestore, admin, invoice, paymentData, comment = "") {
5
+ let bookingNumberDoc = firestore.collection("meta").doc("BOOKING_NUMBER");
6
+
7
+ return firestore
8
+ .runTransaction((transaction) => {
9
+ return bookingNumberDoc.get().then(async (snapshot) => {
10
+ let nextBookingNumber = (snapshot.get("last") ?? 0) + 1;
11
+ let processed = 0;
12
+ comment = [comment, invoice.comment].filter(item => item).join(", ") ?? "";
13
+
14
+ for (let i = 0; i < invoice.items.length; i++) {
15
+ let item = invoice.items[i];
16
+
17
+ // process only product items
18
+ let hasProduct = item.productId?.length > 0;
19
+ let productSnapshot = null;
20
+ let productDescription;
21
+ if (hasProduct) {
22
+ productSnapshot = await firestore.collection("products").doc(item.productId).get();
23
+ productDescription = productSnapshot.get("shortName");
24
+ } else {
25
+ productDescription = item.description;
26
+ }
27
+
28
+ let data = {
29
+ referenceNumber: "TSK" + (nextBookingNumber + processed),
30
+ hint: "",
31
+ specialRequirements: item.dietary?.toUpperCase() ?? "",
32
+ productDescription,
33
+ productRef: hasProduct ? firestore.collection("products").doc(item.productId) : null,
34
+ operator: productSnapshot?.get("operator.default.name") ?? "",
35
+ departureDate: admin.firestore.Timestamp.fromDate(dayjs(item.departureDate).toDate()),
36
+ schedule: item.schedule ?? "",
37
+ pickupTime: "?",
38
+ adults: item.adults ?? 0,
39
+ children: item.children ?? 0,
40
+ infants: item.infants ?? 0,
41
+ leadTraveller: invoice.name,
42
+ email: invoice.email ?? "",
43
+ phoneNumber: invoice.phoneNumber ?? "",
44
+ cc: "",
45
+ customerBookingStatus: "Pending",
46
+ operatorBookingStatus: "Pending",
47
+ sentToOperator: false,
48
+ startingPointDescription: "",
49
+ pickupHotel: item.pickupHotel ?? null,
50
+ roomNumber: item.roomNumber ?? "",
51
+ flight: item.flight ?? null,
52
+ train: item.train ?? null,
53
+ comment,
54
+ customerPaymentStatus: "Paid",
55
+ operatorPaymentStatus: "Pending",
56
+ addOns: item.addOns ?? null,
57
+ labels: ["NEW"],
58
+ paypalInvoiceId: "",
59
+ paypalTransactionId: "",
60
+ omiseChargeId: "", invoiceNumber: "",
61
+ date: admin.firestore.FieldValue.serverTimestamp(),
62
+ createdBy: "Trip Store Krabi Next",
63
+ lastModified: `${dayjs().format("DD.MM.YYYY HH:mm.ss")} by Trip Store Krabi Next`,
64
+ };
65
+
66
+ if (item.startingPointId) {
67
+ data.startingPointId = item.startingPointId;
68
+ }
69
+
70
+ if (item.dropoffHotel && item.dropoffHotel.length > 0) {
71
+ data.dropoffHotel = item.dropoffHotel;
72
+ }
73
+
74
+ if (i > 0) {
75
+ data.retailPrice = 0;
76
+ }
77
+
78
+ if (item.nationalParkFee) {
79
+ data.nationalParkFee = item.nationalParkFee;
80
+ }
81
+
82
+ if (item.tourGuideLanguage) {
83
+ data.tourGuideLanguage = item.tourGuideLanguage;
84
+ }
85
+
86
+ let endPointId = item.endPointId;
87
+ if (!endPointId) {
88
+ let endPoints = productSnapshot?.get("endPoints");
89
+ if (endPoints && endPoints.length === 1) {
90
+ endPointId = endPoints[0].id;
91
+ }
92
+ }
93
+ if (endPointId) {
94
+ data.endPointId = endPointId;
95
+ }
96
+
97
+ if (productSnapshot) {
98
+ data.operatorNetPrice = calculateTotalPrice(data, productSnapshot.data(), false);
99
+ } else {
100
+ data.operatorNetPrice = 0;
101
+ }
102
+
103
+ data = { ...data, ...paymentData };
104
+
105
+ let doc = firestore.collection("bookings").doc();
106
+ transaction.set(doc, data);
107
+ processed++;
108
+ }
109
+
110
+ transaction.set(bookingNumberDoc,
111
+ {
112
+ last: admin.firestore.FieldValue.increment(processed),
113
+ },
114
+ { merge: true }
115
+ );
116
+ });
117
+ });
118
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tripstorekrabi-shared",
3
- "version": "1.8.0",
3
+ "version": "1.9.1",
4
4
  "description": "Shared library for Trip Store Krabi",
5
5
  "author": "Stefan Sommer <tonprince@gmail.com>",
6
6
  "repository": {
@@ -10,13 +10,14 @@
10
10
  "exports": {
11
11
  "./utils": "./utils.js",
12
12
  "./price": "./price.js",
13
- "./availability": "./availability.js"
13
+ "./availability": "./availability.js",
14
+ "./booking": "./booking.js"
14
15
  },
15
16
  "dependencies": {
16
- "dayjs": "^1.11.5"
17
+ "dayjs": "^1.11.6"
17
18
  },
18
19
  "scripts": {
19
20
  "deploy": "npm publish"
20
21
  },
21
22
  "type": "module"
22
- }
23
+ }