-
Notifications
You must be signed in to change notification settings - Fork 7
/
add_credit_card.ts
51 lines (49 loc) · 1.4 KB
/
add_credit_card.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
import {
CreditCardIdConflictError,
CreditCardDataMissing,
UnauthenticatedUserError,
} from "../config/errors";
import { FunctionContext } from "../config/typedefs";
import {
CreditCardCreationRequest,
Promotion,
CreditCard,
} from "../interfaces/interfaces";
import { setPromotion } from "./setters/set_promotion";
import { setCreditCard } from "./setters/set_credit_card";
async function addCreditCardHandler(
data: CreditCardCreationRequest,
context: FunctionContext,
provider
) {
if (context.authenticated) {
if (data.cardData) {
const userId: string = context.uid;
const userRef = provider.getUserRef(userId);
const cardRef = userRef.collection("cards").doc(data.cardData.id);
const cardSnap = await cardRef.get();
if (cardSnap.exists) {
throw CreditCardIdConflictError;
} else {
await setCreditCard(userId, CreditCard.create(data.cardData), provider);
if (data.cardData.promotions) {
for (const promo of data.cardData.promotions) {
if (data.cardData.id) {
await setPromotion(
userId,
data.cardData.id,
Promotion.create(promo),
provider
);
}
}
}
}
} else {
throw CreditCardDataMissing;
}
} else {
throw UnauthenticatedUserError;
}
}
export default addCreditCardHandler;