Skip to content

Commit 2a5bcf3

Browse files
committed
Return removed stuff
1 parent 3de42b7 commit 2a5bcf3

File tree

4 files changed

+104
-28
lines changed

4 files changed

+104
-28
lines changed

src/billing/cloudpayments.ts

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -343,34 +343,42 @@ export default class CloudPaymentsWebhooks {
343343

344344
// let accountId = workspace.accountId;
345345

346-
// try {
347-
// if (!workspace.accountId) {
348-
// accountId = (await context.accounting.createAccount({
349-
// name: `WORKSPACE:${workspace.name}`,
350-
// type: AccountType.LIABILITY,
351-
// currency: Currency.RUB,
352-
// })).recordId;
353-
// await workspace.setAccountId(accountId);
354-
// }
355-
356-
// await context.accounting.payOnce({
357-
// accountId: accountId,
358-
// amount: tariffPlan.monthlyCharge * PENNY_MULTIPLIER,
359-
// description: `Account replenishment to pay for the tariff plan with id ${tariffPlan._id}. CloudPayments transaction ID: ${body.TransactionId}`,
360-
// });
361-
362-
// await context.accounting.purchase({
363-
// accountId,
364-
// amount: tariffPlan.monthlyCharge * PENNY_MULTIPLIER,
365-
// description: `Charging for tariff plan with id ${tariffPlan._id}. CloudPayments transaction ID: ${body.TransactionId}`,
366-
// });
367-
// } catch (e) {
368-
// const error = e as Error;
346+
/*
347+
* try {
348+
* if (!workspace.accountId) {
349+
* accountId = (await context.accounting.createAccount({
350+
* name: `WORKSPACE:${workspace.name}`,
351+
* type: AccountType.LIABILITY,
352+
* currency: Currency.RUB,
353+
* })).recordId;
354+
* await workspace.setAccountId(accountId);
355+
* }
356+
*/
357+
358+
/*
359+
* await context.accounting.payOnce({
360+
* accountId: accountId,
361+
* amount: tariffPlan.monthlyCharge * PENNY_MULTIPLIER,
362+
* description: `Account replenishment to pay for the tariff plan with id ${tariffPlan._id}. CloudPayments transaction ID: ${body.TransactionId}`,
363+
* });
364+
*/
365+
366+
/*
367+
* await context.accounting.purchase({
368+
* accountId,
369+
* amount: tariffPlan.monthlyCharge * PENNY_MULTIPLIER,
370+
* description: `Charging for tariff plan with id ${tariffPlan._id}. CloudPayments transaction ID: ${body.TransactionId}`,
371+
* });
372+
* } catch (e) {
373+
* const error = e as Error;
374+
*/
369375

370376
// this.sendError(res, PayCodes.SUCCESS, `[Billing / Pay] Error while creating operations in accounting ${error.toString()}`, body);
371377

372-
// return;
373-
// }
378+
/*
379+
* return;
380+
* }
381+
*/
374382

375383
try {
376384
await publish('cron-tasks', 'cron-tasks/limiter', JSON.stringify({
@@ -481,7 +489,6 @@ export default class CloudPaymentsWebhooks {
481489
let user;
482490

483491
if (!data.workspaceId || !data.userId || !data.tariffPlanId) {
484-
485492
this.sendError(res, FailCodes.SUCCESS, `[Billing / Fail] No workspace or user id or plan id in request body`, body);
486493

487494
return;

src/resolvers/billingNew.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '@hawk.so/types';
1010
import checksumService from '../utils/checksumService';
1111
import { UserInputError } from 'apollo-server-express';
12+
import cloudPaymentsApi, { CloudPaymentsJsonData } from '../utils/cloudPaymentsApi';
1213

1314
/**
1415
* The amount we will debit to confirm the subscription.
@@ -154,7 +155,7 @@ export default {
154155
throw new UserInputError('There is no saved card with provided id');
155156
}
156157

157-
const jsonData: any = {
158+
const jsonData: CloudPaymentsJsonData = {
158159
checksum: args.input.checksum,
159160
};
160161

@@ -192,7 +193,15 @@ export default {
192193
amount = AMOUNT_FOR_CARD_VALIDATION;
193194
}
194195

195-
const operation = await factories.businessOperationsFactory.getBusinessOperationByTransactionId('foo');
196+
const result = await cloudPaymentsApi.payByToken({
197+
AccountId: user.id,
198+
Amount: amount,
199+
Token: token,
200+
Currency: 'RUB',
201+
JsonData: jsonData,
202+
});
203+
204+
const operation = await factories.businessOperationsFactory.getBusinessOperationByTransactionId(result.Model.TransactionId.toString());
196205

197206
return {
198207
recordId: operation?._id,

src/resolvers/event.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
const EventsFactory = require('../models/eventsFactory');
22
const { ObjectID } = require('mongodb');
3+
const asyncForEach = require('../utils/asyncForEach');
4+
const ProjectToWorkspace = require('../models/projectToWorkspace');
35
const sendPersonalNotification = require('../utils/personalNotifications').default;
6+
const mongo = require('../mongo');
7+
const MongoWatchController = require('../utils/mongoWatchController');
8+
9+
const watchController = new MongoWatchController();
410

511
/**
612
* See all types and fields here {@see ../typeDefs/event.graphql}
@@ -122,6 +128,53 @@ module.exports = {
122128
return release;
123129
},
124130
},
131+
Subscription: {
132+
eventOccurred: {
133+
/**
134+
* Subscribes user to events from his projects
135+
* @param {ResolverObj} _obj
136+
* @param {Object} _args - request variables (not used)
137+
* @param {UserInContext} user - current authorized user {@see ../index.js}
138+
* @param {ContextFactories} factories - factories for working with models
139+
* @return {AsyncIterator<EventSchema>}
140+
*/
141+
subscribe: async (_obj, _args, { user, factories }) => {
142+
const userId = user.id;
143+
const userModel = await factories.usersFactory.findById(userId);
144+
// eslint-disable-next-line no-async-promise-executor
145+
const eventsCollections = new Promise(async resolve => {
146+
// @todo optimize query for getting all user's projects
147+
148+
// Find all user's workspaces
149+
const allWorkspacesIds = await userModel.getWorkspacesIds();
150+
const allProjects = [];
151+
152+
// Find all user's projects
153+
await asyncForEach(allWorkspacesIds, async workspaceId => {
154+
const allProjectsInWorkspace = await new ProjectToWorkspace(workspaceId).getProjects();
155+
156+
allProjects.push(...allProjectsInWorkspace);
157+
});
158+
159+
resolve(allProjects.map(project =>
160+
mongo.databases.events
161+
.collection('events:' + project.id)
162+
));
163+
});
164+
165+
return watchController.getAsyncIteratorForCollectionChangesEvents(eventsCollections);
166+
},
167+
168+
/**
169+
* Sends data to user about new events
170+
* @param {Object} payload - subscription event payload (from mongoDB watch)
171+
* @return {EventSchema}
172+
*/
173+
resolve: (payload) => {
174+
return payload.fullDocument;
175+
},
176+
},
177+
},
125178
Mutation: {
126179
/**
127180
* Mark event as visited for current user

src/typeDefs/event.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,13 @@ type DailyEventInfo {
394394
lastRepetitionTime: Float!
395395
}
396396
397+
type Subscription {
398+
"""
399+
Sends new events from all user projects
400+
"""
401+
eventOccurred: Event! @requireAuth
402+
}
403+
397404
"""
398405
Event information per day with these events
399406
"""

0 commit comments

Comments
 (0)