MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / main

Function main

packages/payments/scripts/sync-subscriptions.ts:122–358  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

120}
121
122async function main() {
123 if (!process.env.POLAR_ACCESS_TOKEN) {
124 throw new Error(
125 'POLAR_ACCESS_TOKEN is missing. Add it to your env before running this script.',
126 );
127 }
128
129 const polar = new Polar({
130 accessToken: process.env.POLAR_ACCESS_TOKEN,
131 server: getServer(),
132 });
133
134 console.log(`Fetching subscriptions from Polar (${getServer()})...`);
135 const subscriptions = targetSubscriptionId
136 ? [await polar.subscriptions.get({ id: targetSubscriptionId })]
137 : await getAllSubscriptions(polar);
138
139 const scopedSubscriptions = subscriptions.filter((subscription) => {
140 if (subscription.productId && IGNORED_PRODUCT_IDS.has(subscription.productId)) {
141 return false;
142 }
143
144 const status = resolveStatus(subscription.status);
145 if (!status) {
146 return false;
147 }
148
149 if (includeCanceled) {
150 return true;
151 }
152
153 return ACTIVE_LIKE_STATUSES.includes(status);
154 });
155
156 console.log(
157 `Found ${subscriptions.length} total subscriptions (${scopedSubscriptions.length} in scope).`,
158 );
159 if (targetSubscriptionId) {
160 console.log(`Filtering by subscription ID: ${targetSubscriptionId}`);
161 }
162 if (isDryRun) {
163 console.log('Running in dry mode: no DB writes will be made.');
164 }
165
166 const productEventsLimit = new Map<string, number | null>();
167 const stats = {
168 updated: 0,
169 unchanged: 0,
170 skippedIgnoredProduct: 0,
171 skippedIgnoredOrg: 0,
172 skippedNoOrg: 0,
173 skippedNoStatus: 0,
174 failed: 0,
175 };
176
177 for (const subscription of subscriptions) {
178 if (subscription.productId && IGNORED_PRODUCT_IDS.has(subscription.productId)) {
179 stats.skippedIgnoredProduct += 1;

Callers 1

Calls 12

getServerFunction · 0.85
getAllSubscriptionsFunction · 0.85
resolveStatusFunction · 0.85
toComparableFunction · 0.85
hasMethod · 0.80
logMethod · 0.45
getMethod · 0.45
warnMethod · 0.45
setMethod · 0.45
pushMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected