chore: Simplify teamUpdater with changes from #6490 (#6492)

This commit is contained in:
Tom Moor
2024-02-04 15:53:14 -08:00
committed by GitHub
parent 930210e46d
commit 7005597aa9
2 changed files with 9 additions and 65 deletions

View File

@@ -4,7 +4,7 @@ import { TeamPreference } from "@shared/types";
import env from "@server/env"; import env from "@server/env";
import { Event, Team, TeamDomain, User } from "@server/models"; import { Event, Team, TeamDomain, User } from "@server/models";
type TeamUpdaterProps = { type Props = {
params: Partial<Omit<Team, "allowedDomains">> & { allowedDomains?: string[] }; params: Partial<Omit<Team, "allowedDomains">> & { allowedDomains?: string[] };
ip?: string; ip?: string;
user: User; user: User;
@@ -12,59 +12,14 @@ type TeamUpdaterProps = {
transaction: Transaction; transaction: Transaction;
}; };
const teamUpdater = async ({ const teamUpdater = async ({ params, user, team, ip, transaction }: Props) => {
params, const { allowedDomains, preferences, subdomain, ...attributes } = params;
user, team.setAttributes(attributes);
team,
ip,
transaction,
}: TeamUpdaterProps) => {
const {
name,
avatarUrl,
subdomain,
sharing,
guestSignin,
documentEmbeds,
memberCollectionCreate,
defaultCollectionId,
defaultUserRole,
inviteRequired,
allowedDomains,
preferences,
} = params;
if (subdomain !== undefined && env.isCloudHosted) { if (subdomain !== undefined && env.isCloudHosted) {
team.subdomain = subdomain === "" ? null : subdomain; team.subdomain = subdomain === "" ? null : subdomain;
} }
if (name) {
team.name = name;
}
if (sharing !== undefined) {
team.sharing = sharing;
}
if (documentEmbeds !== undefined) {
team.documentEmbeds = documentEmbeds;
}
if (guestSignin !== undefined) {
team.guestSignin = guestSignin;
}
if (avatarUrl !== undefined) {
team.avatarUrl = avatarUrl;
}
if (memberCollectionCreate !== undefined) {
team.memberCollectionCreate = memberCollectionCreate;
}
if (defaultCollectionId !== undefined) {
team.defaultCollectionId = defaultCollectionId;
}
if (defaultUserRole !== undefined) {
team.defaultUserRole = defaultUserRole;
}
if (inviteRequired !== undefined) {
team.inviteRequired = inviteRequired;
}
if (allowedDomains !== undefined) { if (allowedDomains !== undefined) {
const existingAllowedDomains = await TeamDomain.findAll({ const existingAllowedDomains = await TeamDomain.findAll({
where: { teamId: team.id }, where: { teamId: team.id },
@@ -101,9 +56,9 @@ const teamUpdater = async ({
(x) => !allowedDomains.includes(x.name) (x) => !allowedDomains.includes(x.name)
); );
await Promise.all(deletedDomains.map((x) => x.destroy({ transaction }))); await Promise.all(deletedDomains.map((x) => x.destroy({ transaction })));
team.allowedDomains = newAllowedDomains; team.allowedDomains = newAllowedDomains;
} }
if (preferences) { if (preferences) {
for (const value of Object.values(TeamPreference)) { for (const value of Object.values(TeamPreference)) {
if (has(preferences, value)) { if (has(preferences, value)) {
@@ -112,25 +67,15 @@ const teamUpdater = async ({
} }
} }
const changes = team.changed(); const changes = team.changeset;
if (Object.keys(changes.attributes).length) {
const savedTeam = await team.save({
transaction,
});
if (changes) {
const data = changes.reduce(
(acc, curr) => ({ ...acc, [curr]: team[curr] }),
{}
);
await Event.create( await Event.create(
{ {
name: "teams.update", name: "teams.update",
actorId: user.id, actorId: user.id,
teamId: user.teamId, teamId: user.teamId,
data,
ip, ip,
changes,
}, },
{ {
transaction, transaction,
@@ -138,7 +83,7 @@ const teamUpdater = async ({
); );
} }
return savedTeam; return team.save({ transaction });
}; };
export default teamUpdater; export default teamUpdater;

View File

@@ -324,7 +324,6 @@ export type IntegrationEvent = BaseEvent<Integration> & {
export type TeamEvent = BaseEvent<Team> & { export type TeamEvent = BaseEvent<Team> & {
name: "teams.create" | "teams.update" | "teams.delete" | "teams.destroy"; name: "teams.create" | "teams.update" | "teams.delete" | "teams.destroy";
data: Partial<Team>;
}; };
export type PinEvent = BaseEvent<Pin> & { export type PinEvent = BaseEvent<Pin> & {