ContributionAmountField now emits cadence-unit values (180 for $180/yr,
15 for $15/mo). Server endpoints were still multiplying annual by 12,
which would have charged $2160/yr instead of $180/yr after the form
ports in Tasks 2–3.
- helcim/subscription.post.js: recurringAmount = contributionAmount
(no more × 12 for annual)
- members/update-contribution.post.js: same drop in both Case 1
(free→paid) and Case 3 (paid→paid)
- slack.ts notifyNewMember: new positional `cadence` param so the
Slack notification suffix renders /yr or /mo instead of hardcoded
/month; all three call sites updated to pass member.billingCadence
- tests updated to match the new contract:
- helcim-subscription.test.js: annual tests now send the cadence-
unit amount (180, 600) and expect the same recurringAmount
- update-contribution.test.js: annual Case 1 and Case 3 tests
updated likewise
263 lines
7.3 KiB
TypeScript
263 lines
7.3 KiB
TypeScript
import { WebClient } from "@slack/web-api";
|
|
|
|
export class SlackService {
|
|
private client: WebClient;
|
|
private vettingChannelId: string;
|
|
|
|
constructor(botToken: string, vettingChannelId: string) {
|
|
this.client = new WebClient(botToken);
|
|
this.vettingChannelId = vettingChannelId;
|
|
}
|
|
|
|
/**
|
|
* Find user in workspace by email
|
|
*/
|
|
async findUserByEmail(email: string): Promise<string | null> {
|
|
try {
|
|
const response = await this.client.users.lookupByEmail({ email });
|
|
return response.user?.id || null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Find user ID by username (display name or real name)
|
|
*/
|
|
async findUserIdByUsername(username: string): Promise<string | null> {
|
|
try {
|
|
const cleanUsername = username.replace("@", "").toLowerCase();
|
|
|
|
// List all users and search for matching username
|
|
const response = await this.client.users.list();
|
|
|
|
if (!response.members) {
|
|
return null;
|
|
}
|
|
|
|
// Search for user by name or display_name
|
|
const user = response.members.find((member: any) => {
|
|
const name = member.name?.toLowerCase() || "";
|
|
const realName = member.real_name?.toLowerCase() || "";
|
|
const displayName = member.profile?.display_name?.toLowerCase() || "";
|
|
|
|
return (
|
|
name === cleanUsername ||
|
|
displayName === cleanUsername ||
|
|
realName.includes(cleanUsername)
|
|
);
|
|
});
|
|
|
|
return user?.id || null;
|
|
} catch (error) {
|
|
console.error("Error looking up Slack user by username:", error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Open/get a DM channel with a user and return the channel ID
|
|
* This creates or opens a DM conversation and returns the channel ID (starts with D)
|
|
*/
|
|
async openDMChannel(userId: string): Promise<string | null> {
|
|
try {
|
|
const response = await this.client.conversations.open({
|
|
users: userId,
|
|
});
|
|
|
|
if (response.ok && response.channel?.id) {
|
|
console.log(
|
|
`Opened DM channel for user ${userId}: ${response.channel.id}`,
|
|
);
|
|
return response.channel.id;
|
|
}
|
|
|
|
return null;
|
|
} catch (error: any) {
|
|
console.error(
|
|
"Error opening DM channel:",
|
|
error.data?.error || error.message,
|
|
);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send a notification to the vetting channel about a new member
|
|
*/
|
|
async notifyNewMember(
|
|
memberName: string,
|
|
memberEmail: string,
|
|
circle: string,
|
|
contributionAmount: number,
|
|
cadence: string = 'monthly',
|
|
invitationStatus: string = "manual_invitation_required",
|
|
): Promise<void> {
|
|
try {
|
|
let statusMessage = "";
|
|
let actionMessage = "";
|
|
|
|
switch (invitationStatus) {
|
|
case "existing_user_added_to_channel":
|
|
statusMessage =
|
|
"✅ Existing user automatically added to this channel.";
|
|
actionMessage = "Ready for vetting!";
|
|
break;
|
|
case "user_already_in_channel":
|
|
statusMessage = "✅ User is already in this channel.";
|
|
actionMessage = "Ready for vetting!";
|
|
break;
|
|
case "new_user_invited_to_workspace":
|
|
statusMessage =
|
|
"🎉 User successfully invited to workspace as single-channel guest.";
|
|
actionMessage = "Ready for vetting!";
|
|
break;
|
|
case "manual_invitation_required":
|
|
statusMessage = "📧 User needs to be manually invited to join Slack.";
|
|
actionMessage = `Please vet this new member before inviting them to other channels.`;
|
|
break;
|
|
default:
|
|
statusMessage = "⚠️ Invitation status unknown.";
|
|
actionMessage = "Manual review required.";
|
|
}
|
|
|
|
await this.client.chat.postMessage({
|
|
channel: this.vettingChannelId,
|
|
text: `New Ghost Guild member: ${memberName}`,
|
|
blocks: [
|
|
{
|
|
type: "header",
|
|
text: {
|
|
type: "plain_text",
|
|
text: "New Ghost Guild Member Registration",
|
|
},
|
|
},
|
|
{
|
|
type: "section",
|
|
fields: [
|
|
{
|
|
type: "mrkdwn",
|
|
text: `*Name:*\n${memberName}`,
|
|
},
|
|
{
|
|
type: "mrkdwn",
|
|
text: `*Email:*\n${memberEmail}`,
|
|
},
|
|
{
|
|
type: "mrkdwn",
|
|
text: `*Circle:*\n${circle}`,
|
|
},
|
|
{
|
|
type: "mrkdwn",
|
|
text: `*Contribution:*\n$${contributionAmount}/${cadence === 'annual' ? 'yr' : 'mo'}`,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
type: "section",
|
|
text: {
|
|
type: "mrkdwn",
|
|
text: `*Status:* ${statusMessage}\n*Action:* ${actionMessage}`,
|
|
},
|
|
},
|
|
{
|
|
type: "divider",
|
|
},
|
|
],
|
|
});
|
|
} catch (error) {
|
|
console.error("Failed to send Slack notification:", error);
|
|
// Don't throw - this is non-critical
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a new Slack channel. Returns the new channel id and normalized name.
|
|
*/
|
|
async createChannel(
|
|
name: string,
|
|
isPrivate: boolean = false,
|
|
): Promise<{ id: string; name: string }> {
|
|
const normalized = name
|
|
.trim()
|
|
.replace(/^#/, '')
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9_-]+/g, '-')
|
|
.replace(/^-+|-+$/g, '')
|
|
.slice(0, 80)
|
|
|
|
const response = await this.client.conversations.create({
|
|
name: normalized,
|
|
is_private: isPrivate,
|
|
})
|
|
|
|
if (!response.ok || !response.channel?.id || !response.channel?.name) {
|
|
throw new Error(`Slack create failed: ${response.error || 'unknown'}`)
|
|
}
|
|
|
|
return { id: response.channel.id, name: response.channel.name }
|
|
}
|
|
|
|
/**
|
|
* Verify the Slack channel exists and bot has access
|
|
*/
|
|
async verifyChannelAccess(): Promise<boolean> {
|
|
try {
|
|
const response = await this.client.conversations.info({
|
|
channel: this.vettingChannelId,
|
|
});
|
|
return response.ok && !!response.channel;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get configured Slack service instance
|
|
*/
|
|
export function getSlackService(): SlackService | null {
|
|
const config = useRuntimeConfig();
|
|
|
|
if (!config.slackBotToken || !config.slackVettingChannelId) {
|
|
console.warn(
|
|
"Slack integration not configured - missing bot token or channel ID",
|
|
);
|
|
return null;
|
|
}
|
|
|
|
return new SlackService(config.slackBotToken, config.slackVettingChannelId);
|
|
}
|
|
|
|
/**
|
|
* Get a SlackService for operations that don't need the vetting channel.
|
|
*/
|
|
export function getSlackServiceNoVetting(): SlackService | null {
|
|
const config = useRuntimeConfig();
|
|
|
|
if (!config.slackBotToken) {
|
|
console.warn("Slack integration not configured - missing bot token");
|
|
return null;
|
|
}
|
|
|
|
return new SlackService(config.slackBotToken, "");
|
|
}
|
|
|
|
/**
|
|
* Get a SlackService backed by the AdminGhost app token for admin-only
|
|
* operations like channel creation. Falls back to the main bot token if
|
|
* AdminGhost isn't configured.
|
|
*/
|
|
export function getSlackAdminService(): SlackService | null {
|
|
const config = useRuntimeConfig();
|
|
|
|
const token = config.slackAdminBotToken || config.slackBotToken;
|
|
if (!token) {
|
|
console.warn(
|
|
"Slack admin integration not configured - missing admin bot token",
|
|
);
|
|
return null;
|
|
}
|
|
|
|
return new SlackService(token, "");
|
|
}
|