Skip to content

Commit

Permalink
Merge pull request #1095 from rockingrohit9639/feature/owner-team-member
Browse files Browse the repository at this point in the history
Feature/owner team member
  • Loading branch information
DonKoko authored Jun 25, 2024
2 parents 63c4950 + 7ec5870 commit 2d539f1
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- It is used to insert team members for all organization owners who do not have a team member record.
-- To test if the migration works, you can run the same query without the first line (INSERT INTO "TeamMember" ...) and check the results.
INSERT INTO "TeamMember" (id, name, "organizationId", "userId", "createdAt", "updatedAt")
SELECT
'c' || md5(random()::text || clock_timestamp()::text)::uuid, -- Simulates cuid generation
COALESCE(u."firstName", '') || ' ' || COALESCE(u."lastName", ''), -- Combines first name and last name, replacing NULL with empty string
o.id, -- Organization ID
o."userId", -- User ID
NOW(), -- Current timestamp for createdAt
NOW() -- Current timestamp for updatedAt
FROM
"Organization" o
LEFT JOIN "TeamMember" tm ON o.id = tm."organizationId" AND o."userId" = tm."userId"
JOIN "User" u ON o."userId" = u.id
WHERE
tm.id IS NULL;
1 change: 1 addition & 0 deletions app/modules/asset/service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,7 @@ export async function createAssetsFromContentImport({
customFieldsValues,
});
} catch (error) {
// eslint-disable-next-line no-console
console.error("Error processing asset", error);
// Handle the error as needed
}
Expand Down
14 changes: 13 additions & 1 deletion app/modules/organization/service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export async function createOrganization({
image: File | null;
}) {
try {
const owner = await db.user.findFirstOrThrow({ where: { id: userId } });

const data = {
name,
currency,
Expand All @@ -132,7 +134,17 @@ export async function createOrganization({
id: userId,
},
},
};
/**
* Creating a teamMember when a new organization/workspace is created
* so that the owner appear in the list by default
*/
members: {
create: {
name: `${owner.firstName} ${owner.lastName} (Owner)`,
user: { connect: { id: owner.id } },
},
},
} satisfies Prisma.OrganizationCreateInput;

const org = await db.organization.create({ data });

Expand Down
67 changes: 22 additions & 45 deletions app/modules/team-member/service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,59 +212,36 @@ export async function getTeamMemberForCustodianFilter({
getAll?: boolean;
}) {
try {
const [
teamMemberExcludedSelected,
teamMembersSelected,
totalTeamMembers,
org,
] = await Promise.all([
db.teamMember.findMany({
where: {
organizationId,
id: { notIn: selectedTeamMembers },
deletedAt: null,
},
include: {
user: {
select: {
firstName: true,
lastName: true,
email: true,
const [teamMemberExcludedSelected, teamMembersSelected, totalTeamMembers] =
await Promise.all([
db.teamMember.findMany({
where: {
organizationId,
id: { notIn: selectedTeamMembers },
deletedAt: null,
},
include: {
user: {
select: {
firstName: true,
lastName: true,
email: true,
},
},
},
},
take: getAll ? undefined : 12,
}),
db.teamMember.findMany({
where: { organizationId, id: { in: selectedTeamMembers } },
}),
db.teamMember.count({ where: { organizationId, deletedAt: null } }),
db.organization.findUnique({
where: { id: organizationId },
select: { owner: true },
}),
]);
take: getAll ? undefined : 12,
}),
db.teamMember.findMany({
where: { organizationId, id: { in: selectedTeamMembers } },
}),
db.teamMember.count({ where: { organizationId, deletedAt: null } }),
]);

const allTeamMembers = [
...teamMembersSelected,
...teamMemberExcludedSelected,
];

/**
* Owners can be assigned in bookings so have to add it to the list
*/
if (org?.owner && typeof org.owner.id === "string") {
allTeamMembers.push({
id: "owner",
name: `${org.owner.firstName} ${org.owner.lastName} (Owner)`,
userId: org.owner.id,
organizationId,
createdAt: new Date(),
updatedAt: new Date(),
deletedAt: null,
});
}

/**
* If teamMember has a user associated then we have to use that user's id
* otherwise we have to use teamMember's id
Expand Down
6 changes: 6 additions & 0 deletions app/modules/user/service.server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ describe(createUserAccountForTesting.name, () => {
userId: USER_ID,
})),
},
members: {
create: {
name: `${undefined} ${undefined} (Owner)`,
user: { connect: { id: USER_ID } },
},
},
},
],
},
Expand Down
23 changes: 17 additions & 6 deletions app/modules/user/service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,11 @@ export async function createUser(
username,
firstName,
lastName,
roles: {
connect: {
name: Roles["USER"],
},
},
...(!isSSO && {
organizations: {
create: [
Expand All @@ -456,15 +461,20 @@ export async function createUser(
userId,
})),
},
/**
* Creating a teamMember when a new organization/workspace is created
* so that the owner appear in the list by default
*/
members: {
create: {
name: `${firstName} ${lastName} (Owner)`,
user: { connect: { id: userId } },
},
},
},
],
},
}),
roles: {
connect: {
name: Roles["USER"],
},
},
...(isSSO && {
// When user is coming from SSO, we set them as onboarded as we already have their first and last name and they dont need a password.
onboarded: true,
Expand All @@ -477,7 +487,8 @@ export async function createUser(
},
});

/** Create user organization association
/**
* Creating an organization for the user
* 1. For the personal org
* 2. For the org that the user is being attached to
*/
Expand Down

0 comments on commit 2d539f1

Please sign in to comment.