gristlabs_grist-core/app/common/ShareAnnotator.ts
Jarosław Sadziński 60423edc17 (core) Customizable stripe plans.
Summary:
- Reading plans from Stripe, and allowing Stripe to define custom plans.
- Storing product features (aka limits) in Stripe, that override those in db.
- Adding hierarchical data in Stripe. All features are defined at Product level but can be overwritten on Price levels.
- New options for Support user to
-- Override product for team site (if he is added as a billing manager)
-- Override subscription and customer id for a team site
-- Attach an "offer", an custom plan configured in stripe that a team site can use
-- Enabling wire transfer for subscription by allowing subscription to be created without a payment method (which is customizable)

Test Plan: Updated and new.

Reviewers: georgegevoian

Reviewed By: georgegevoian

Differential Revision: https://phab.getgrist.com/D4201
2024-05-19 09:09:19 +02:00

105 lines
3.2 KiB
TypeScript

import { Features } from 'app/common/Features';
import { normalizeEmail } from 'app/common/emails';
import { PermissionData, PermissionDelta } from 'app/common/UserAPI';
/**
* Mark that the share is share number #at of a maximum of #top. The #at values
* start at 1.
*/
export interface ShareLimitAnnotation {
at: number;
top?: number;
}
/**
* Some facts about a share.
*/
export interface ShareAnnotation {
isMember?: boolean; // Is the share for a team member.
isSupport?: boolean; // Is the share for a support user.
collaboratorLimit?: ShareLimitAnnotation; // Does the share count towards a collaborator limit.
}
/**
* Facts about all shares for a resource.
*/
export interface ShareAnnotations {
hasTeam?: boolean; // Is the resource in a team site?
users: Map<string, ShareAnnotation>; // Annotations keyed by normalized user email.
}
export interface ShareAnnotatorOptions {
supportEmail?: string; // Known email address of the support user (e.g. support@getgrist.com).
}
/**
* Helper for annotating users mentioned in a proposed change of shares, given the
* current shares in place.
*/
export class ShareAnnotator {
private _supportEmail = this._options.supportEmail;
constructor(
private _features: Features|null,
private _state: PermissionData,
private _options: ShareAnnotatorOptions = {}
) {
}
public updateState(state: PermissionData) {
this._state = state;
}
public annotateChanges(change: PermissionDelta): ShareAnnotations {
const features = this._features ?? {};
const annotations: ShareAnnotations = {
hasTeam: !this._features || this._features.vanityDomain,
users: new Map(),
};
if (features.maxSharesPerDocPerRole || features.maxSharesPerWorkspace) {
// For simplicity, don't try to annotate if limits not used at the time of writing
// are in place.
return annotations;
}
const top = features.maxSharesPerDoc;
let at = 0;
const makeAnnotation = (user: {email: string, isMember?: boolean, isSupport?: boolean, access: string|null}) => {
const annotation: ShareAnnotation = {
isMember: user.isMember,
};
if (user.isSupport) {
return { isSupport: true };
}
if (!annotation.isMember && user.access) {
at++;
annotation.collaboratorLimit = {
at,
top
};
}
return annotation;
};
const removed = new Set(
Object.entries(change?.users||{}).filter(([, v]) => v === null)
.map(([k, ]) => normalizeEmail(k)));
for (const user of this._state.users) {
if (removed.has(user.email)) { continue; }
if (!user.isMember && !user.access) { continue; }
annotations.users.set(user.email, makeAnnotation(user));
}
const tweaks = new Set(
Object.entries(change?.users||{}).filter(([, v]) => v !== null)
.map(([k, ]) => normalizeEmail(k)));
for (const email of tweaks) {
const annotation = annotations.users.get(email) || makeAnnotation({
email,
isMember: false,
isSupport: Boolean(email.trim() !== '' && email === this._supportEmail),
access: '<set>',
});
annotations.users.set(email, annotation);
}
return annotations;
}
}