Files
calendar/apps/ai/src/utils/extractUsers.ts
T
3047b5319b feat: cal.ai v1.2.0 (#11868)
* bump nextjs version in ai

* lowercase username and email

* onboarding email

* direct user to install app if not installed

* multiple suggested times for link flow

* summary of context prompt engineering

* specify the @username nuance and discourage Ids

* v1.2.0

* Update README

* Change title

* simplify and improve booking link flow

* add build:ai to package.json

* better onboarding copy

* onboarding touches

* remove console logs and temp hacks

* remove env vars in app store and token in AI app

* invited user id should be string

---------

Co-authored-by: tedspare <ted.spare@gmail.com>
2023-10-14 09:52:24 +01:00

86 lines
2.0 KiB
TypeScript

import prisma from "@calcom/prisma";
import type { UserList } from "../types/user";
/*
* Extracts usernames (@Example) and emails (hi@example.com) from a string
*/
export const extractUsers = async (text: string) => {
const usernames = text
.match(/(?<![a-zA-Z0-9_.])@[a-zA-Z0-9_]+/g)
?.map((username) => username.slice(1).toLowerCase());
const emails = text
.match(/[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+/g)
?.map((email) => email.toLowerCase());
const dbUsersFromUsernames = usernames
? await prisma.user.findMany({
select: {
id: true,
username: true,
email: true,
},
where: {
username: {
in: usernames,
},
},
})
: [];
const usersFromUsernames = usernames
? usernames.map((username) => {
const user = dbUsersFromUsernames.find((u) => u.username === username);
return user
? {
username,
id: user.id,
email: user.email,
type: "fromUsername",
}
: {
username,
id: null,
email: null,
type: "fromUsername",
};
})
: [];
const dbUsersFromEmails = emails
? await prisma.user.findMany({
select: {
id: true,
email: true,
username: true,
},
where: {
email: {
in: emails,
},
},
})
: [];
const usersFromEmails = emails
? emails.map((email) => {
const user = dbUsersFromEmails.find((u) => u.email === email);
return user
? {
email,
id: user.id,
username: user.username,
type: "fromEmail",
}
: {
email,
id: null,
username: null,
type: "fromEmail",
};
})
: [];
return [...usersFromUsernames, ...usersFromEmails] as UserList;
};