-
Notifications
You must be signed in to change notification settings - Fork 7
/
edit_credit_card.ts
35 lines (33 loc) · 986 Bytes
/
edit_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
import { FunctionContext } from "../config/typedefs";
import { CreditCardUpdateRequest, CreditCard } from "../interfaces/interfaces";
import { setCreditCard } from "./setters/set_credit_card";
import {
UnauthenticatedUserError,
CreditCardNotExistError,
} from "../config/errors";
async function editCreditCardHandler(
data: CreditCardUpdateRequest,
context: FunctionContext,
provider
) {
if (context.authenticated) {
const userId: string = context.uid;
const userRef = provider.getUserRef(userId);
const cardRef = userRef.collection("cards").doc(data.updatedCardData?.id);
const cardSnap = await cardRef.get();
if (!cardSnap.exists) {
throw CreditCardNotExistError;
} else {
if (data.updatedCardData) {
await setCreditCard(
userId,
CreditCard.create(data.updatedCardData),
provider
);
}
}
} else {
throw UnauthenticatedUserError;
}
}
export default editCreditCardHandler;