-
Notifications
You must be signed in to change notification settings - Fork 10
/
callbacks.js
112 lines (101 loc) · 3.07 KB
/
callbacks.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { Connectors, updateMutator } from "meteor/vulcan:core";
import moment from "moment";
import Posts from "../../modules/posts/collection";
import Discounts from "../../modules/discounts/collection";
import { postStatus } from "../../modules/data";
import get from "lodash/get";
import VulcanEmail from "meteor/vulcan:email";
import { getTwitterData } from "../helpers/twitter";
export const getTwitterMetadata = async (data) => {
const twitterScreenName = data.twitterScreenName || data.credit;
if (!twitterScreenName) {
return data;
} else {
const twitterData = await getTwitterData(twitterScreenName);
return { ...data, ...twitterData };
}
};
export const checkForScheduledAt = async (validationErrors, { data }) => {
if (data.isSponsored && !data.scheduledAt) {
validationErrors.push({
id: "app.missing_scheduled_at",
path: "scheduledAt",
});
}
return validationErrors;
};
export const checkForDuplicates = async (
validationErrors,
{ document, data }
) => {
const url = data.url;
const _id = document._id || data._id;
// if there is no URL, abort check
if (!url) {
return validationErrors;
}
// check that there are no previous published posts with the same link in the past 6 months
const sixMonthsAgo = moment().subtract(6, "months").toDate();
const selector = {
url,
status: postStatus.published,
postedAt: { $gte: sixMonthsAgo },
};
// if we are updating a post, exclude itself from the check
if (_id) {
selector._id = { $ne: _id };
}
const duplicatePost = await Connectors.get(Posts, selector);
if (duplicatePost) {
validationErrors.push({
id: "app.duplicate_post",
path: "url",
properties: {
duplicatePostId: duplicatePost._id,
},
});
}
return validationErrors;
};
export const setDiscountAmount = async (data, { document }) => {
const { discountCode, isSponsored } = data;
if (isSponsored && discountCode) {
const discount = await Connectors.get(Discounts, { code: discountCode });
if (discount) {
data.discountAmount = discount.amount || 0;
}
}
return data;
};
export const setPostPaidAt = (data, { context }) => {
// only trigger when we're in the process of creating a Stripe charge
if (context.event === "stripe.process.sync") {
data.paidAt = new Date();
data.status = postStatus.scheduled; // mark as paid
data.sponsorshipPrice = get(context, "chargeDoc.data.amount");
}
return data;
};
export const sendSponsorshipPaymentNotification = async ({
document: post,
context,
}) => {
// only trigger when we're in the process of creating a Stripe charge
if (context.event === "stripe.process.sync") {
await VulcanEmail.buildAndSend({
emailName: "postApproved",
variables: { input: { id: post._id } },
});
}
};
// export const tweetLink = async ({ document: post }) => {
// console.log('// tweetLink')
// if (!post.tweetedAt) {
// createTweet
// await updateMutator({
// collectton: Posts,
// documentId: post._id,
// data: { tweetedAt: new Date() }
// });
// }
// }