-
Notifications
You must be signed in to change notification settings - Fork 7
/
set_promotion.ts
33 lines (32 loc) · 916 Bytes
/
set_promotion.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
import { Promotion } from "../../interfaces/interfaces";
import Provider from "../../provider";
import {
PromotionIdMissingError,
CreditCardNotExistError,
PromotionAlreadyExistError,
} from "../../config/errors";
export async function setPromotion(
userId: string,
targetCardId: string,
promotion: Promotion,
provider: Provider
) {
const userRef = provider.getUserRef(userId);
const cardRef = userRef.collection("cards").doc(targetCardId);
const cardSnap = await cardRef.get();
if (cardSnap.exists) {
if (promotion.id) {
const promoRef = cardRef.collection("promos").doc(promotion.id);
const promoSnap = await promoRef.get();
if (promoSnap.exists) {
throw PromotionAlreadyExistError;
} else {
await promoRef.set(promotion.toJSON());
}
} else {
throw PromotionIdMissingError;
}
} else {
throw CreditCardNotExistError;
}
}