Skip to content

Commit

Permalink
better handling of merging profiles with db properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcasey committed Oct 22, 2024
1 parent d0bbd14 commit 94ffcc2
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { log } from '@charmverse/core/log';
import styled from '@emotion/styled';
import CloseIcon from '@mui/icons-material/Close';
import type { AutocompleteRenderGetTagProps } from '@mui/material';
Expand Down Expand Up @@ -198,6 +199,11 @@ export function UserSelect({
const [isOpen, setIsOpen] = useState(defaultOpened);
const { membersRecord } = useMembers();

const members = memberIds.map((id) => membersRecord[id]).filter(isTruthy);
if (members.length !== memberIds.length) {
log.warn('Missing profile for some members', { memberIds: memberIds.filter((id) => !membersRecord[id]) });
}

const _onChange = useCallback(
(newMemberIds: string[]) => {
if (!readOnly && ((disallowEmpty && newMemberIds.length !== 0) || !disallowEmpty)) {
Expand Down
88 changes: 87 additions & 1 deletion lib/users/mergeProfiles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { log } from '@charmverse/core/log';
import { prisma } from '@charmverse/core/prisma-client';

import type { BoardFields } from 'lib/databases/board';
import type { CardFields } from 'lib/databases/card';

// Please read before running. Not battle-tested
export async function mergeProfiles({
primaryProfileId,
Expand Down Expand Up @@ -74,7 +78,8 @@ export async function mergeProfiles({
id: secondaryProfileId
},
data: {
deletedAt: new Date()
deletedAt: new Date(),
username: `Merged with user id: ${primaryProfileId}`
}
}),
prisma.spaceRole.updateMany({
Expand Down Expand Up @@ -278,6 +283,15 @@ export async function mergeProfiles({
}
})
]);

for (const spaceRole of secondary.spaceRoles) {
await migratePersonPropertyValues({
spaceId: spaceRole.spaceId,
primaryId: primaryProfileId,
secondaryId: secondaryProfileId
});
}

log.debug('results', {
'page.createdBy': results[0],
'page.updatedBy': results[1],
Expand Down Expand Up @@ -310,3 +324,75 @@ export async function mergeProfiles({
'discordUser.userId': results[28]
});
}

// This function is used to migrate person property values from one user to another by space
async function migratePersonPropertyValues({
spaceId,
primaryId,
secondaryId
}: {
spaceId: string;
primaryId: string;
secondaryId: string;
}) {
const boards = await prisma.block.findMany({
where: {
spaceId,
type: {
in: ['board', 'inline_board', 'inline_linked_board', 'linked_board']
}
}
});

const boardsWithPeopleProperties = boards
.map((b) => ({
boardId: b.id,
personProperties: (b.fields as unknown as BoardFields)?.cardProperties.filter((p) => p.type === 'person')
}))
.filter((r) => r.personProperties.length > 0);

// console.log('Found boards with person propertiesto check', { spaceId, boards: boardsWithPeopleProperties.length });

for (const board of boardsWithPeopleProperties) {
const cards = await prisma.block.findMany({
where: {
parentId: board.boardId,
type: 'card'
}
});
const cardsToUpdate = cards
.map((c) => {
const fields = { ...(c.fields as unknown as CardFields) };
let updated = false;
board.personProperties.forEach((e) => {
const personValue = fields.properties[e.id];
if (personValue === secondaryId) {
fields.properties[e.id] = primaryId;
updated = true;
} else if (Array.isArray(personValue) && personValue.includes(secondaryId)) {
fields.properties[e.id] = personValue.map((p) => (p === secondaryId ? primaryId : p));
updated = true;
}
});
if (updated) {
return {
cardId: c.id,
fields
};
}
return null;
})
.filter(Boolean);
if (cardsToUpdate.length > 0) {
log.debug(`Updating ${cardsToUpdate.length} database cards`);
await prisma.$transaction(
cardsToUpdate.map((card) =>
prisma.block.update({
where: { id: card!.cardId },
data: { fields: card!.fields }
})
)
);
}
}
}

0 comments on commit 94ffcc2

Please sign in to comment.