Added Team with 'pro' and 'free' as members (#1063)
* Added Team with 'pro' and 'free' as members * Teams fundamentally changes event-type behaviour and requires seperate users
This commit is contained in:
		
							parent
							
								
									5af159cf4e
								
							
						
					
					
						commit
						94f3ae1c64
					
				
					 1 changed files with 87 additions and 4 deletions
				
			
		| 
						 | 
				
			
			@ -1,4 +1,4 @@
 | 
			
		|||
import { Prisma, PrismaClient, UserPlan } from "@prisma/client";
 | 
			
		||||
import { MembershipRole, Prisma, PrismaClient, UserPlan } from "@prisma/client";
 | 
			
		||||
import dayjs from "dayjs";
 | 
			
		||||
import { uuid } from "short-uuid";
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -34,8 +34,9 @@ async function createUserAndEventType(opts: {
 | 
			
		|||
  });
 | 
			
		||||
 | 
			
		||||
  console.log(
 | 
			
		||||
    `👤 Upserted '${opts.user.username}' with email "${opts.user.email}" & password "${opts.user.password}". Booking page 👉 http://localhost:3000/${opts.user.username}`
 | 
			
		||||
    `👤 Upserted '${opts.user.username}' with email "${opts.user.email}" & password "${opts.user.password}". Booking page 👉 ${process.env.BASE_URL}/${opts.user.username}`
 | 
			
		||||
  );
 | 
			
		||||
 | 
			
		||||
  for (const eventTypeInput of opts.eventTypes) {
 | 
			
		||||
    const { _bookings: bookingInputs = [], ...eventTypeData } = eventTypeInput;
 | 
			
		||||
    eventTypeData.userId = user.id;
 | 
			
		||||
| 
						 | 
				
			
			@ -57,7 +58,7 @@ async function createUserAndEventType(opts: {
 | 
			
		|||
 | 
			
		||||
    if (eventType) {
 | 
			
		||||
      console.log(
 | 
			
		||||
        `\t📆 Event type ${eventTypeData.slug} already seems seeded - http://localhost:3000/${user.username}/${eventTypeData.slug}`
 | 
			
		||||
        `\t📆 Event type ${eventTypeData.slug} already seems seeded - ${process.env.BASE_URL}/${user.username}/${eventTypeData.slug}`
 | 
			
		||||
      );
 | 
			
		||||
      continue;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -66,7 +67,7 @@ async function createUserAndEventType(opts: {
 | 
			
		|||
    });
 | 
			
		||||
 | 
			
		||||
    console.log(
 | 
			
		||||
      `\t📆 Event type ${eventTypeData.slug}, length ${eventTypeData.length}min - http://localhost:3000/${user.username}/${eventTypeData.slug}`
 | 
			
		||||
      `\t📆 Event type ${eventTypeData.slug}, length ${eventTypeData.length}min - ${process.env.BASE_URL}/${user.username}/${eventTypeData.slug}`
 | 
			
		||||
    );
 | 
			
		||||
    for (const bookingInput of bookingInputs) {
 | 
			
		||||
      await prisma.booking.create({
 | 
			
		||||
| 
						 | 
				
			
			@ -99,6 +100,49 @@ async function createUserAndEventType(opts: {
 | 
			
		|||
      );
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return user;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function createTeamAndAddUsers(
 | 
			
		||||
  teamInput: Prisma.TeamCreateInput,
 | 
			
		||||
  users: { id: number; username: string; role?: MembershipRole }[]
 | 
			
		||||
) {
 | 
			
		||||
  const createTeam = async (team: Prisma.TeamCreateInput) => {
 | 
			
		||||
    try {
 | 
			
		||||
      return await prisma.team.create({
 | 
			
		||||
        data: {
 | 
			
		||||
          ...team,
 | 
			
		||||
        },
 | 
			
		||||
      });
 | 
			
		||||
    } catch (_err) {
 | 
			
		||||
      if (_err instanceof Error && _err.message.indexOf("Unique constraint failed on the fields") !== -1) {
 | 
			
		||||
        console.log(`Team '${team.name}' already exists, skipping.`);
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
      throw _err;
 | 
			
		||||
    }
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  const team = await createTeam(teamInput);
 | 
			
		||||
  if (!team) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  console.log(`🏢 Created team '${teamInput.name}' - ${process.env.BASE_URL}/team/${team.slug}`);
 | 
			
		||||
 | 
			
		||||
  for (const user of users) {
 | 
			
		||||
    const { role = MembershipRole.OWNER, id, username } = user;
 | 
			
		||||
    await prisma.membership.create({
 | 
			
		||||
      data: {
 | 
			
		||||
        teamId: team.id,
 | 
			
		||||
        userId: id,
 | 
			
		||||
        role: role,
 | 
			
		||||
        accepted: true,
 | 
			
		||||
      },
 | 
			
		||||
    });
 | 
			
		||||
    console.log(`\t👤 Added '${teamInput.name}' membership for '${username}' with role '${role}'`);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function main() {
 | 
			
		||||
| 
						 | 
				
			
			@ -218,6 +262,45 @@ async function main() {
 | 
			
		|||
    ],
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  const freeUserTeam = await createUserAndEventType({
 | 
			
		||||
    user: {
 | 
			
		||||
      email: "teamfree@example.com",
 | 
			
		||||
      password: "teamfree",
 | 
			
		||||
      username: "teamfree",
 | 
			
		||||
      name: "Team Free Example",
 | 
			
		||||
      plan: "FREE",
 | 
			
		||||
    },
 | 
			
		||||
    eventTypes: [],
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  const proUserTeam = await createUserAndEventType({
 | 
			
		||||
    user: {
 | 
			
		||||
      email: "teampro@example.com",
 | 
			
		||||
      password: "teampro",
 | 
			
		||||
      username: "teampro",
 | 
			
		||||
      name: "Team Pro Example",
 | 
			
		||||
      plan: "PRO",
 | 
			
		||||
    },
 | 
			
		||||
    eventTypes: [],
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  await createTeamAndAddUsers(
 | 
			
		||||
    {
 | 
			
		||||
      name: "Seeded Team",
 | 
			
		||||
      slug: "seeded-team",
 | 
			
		||||
    },
 | 
			
		||||
    [
 | 
			
		||||
      {
 | 
			
		||||
        id: proUserTeam.id,
 | 
			
		||||
        username: proUserTeam.name || "Unknown",
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        id: freeUserTeam.id,
 | 
			
		||||
        username: freeUserTeam.name || "Unknown",
 | 
			
		||||
      },
 | 
			
		||||
    ]
 | 
			
		||||
  );
 | 
			
		||||
 | 
			
		||||
  await prisma.$disconnect();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue