Last active
May 25, 2022 10:32
-
-
Save RoyalIcing/abe56357072fa7e14f456018e484ae25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See: https://twitter.com/buildsghost/status/1529258931809202176 | |
// And: https://twitter.com/lewisl9029/status/1529360924343095296 | |
let db | |
function allProps(object) { | |
Object.fromEntries( | |
await Promise.all( | |
Object.entries(object).map(async ([key, promise]) => { | |
return [key, await promise] | |
}), | |
), | |
) | |
} | |
async function loader({ request }) { | |
let session = await requireSession(request) | |
let userId = session.userId | |
let { | |
user, | |
personalBinder, | |
anyAdminOrgMemberships, | |
anyNonAdminOrgMemberships, | |
anyBinderMemberships, | |
} = await allProps({ | |
user: db.user.findUnique({ where: { userId } }), | |
personalBinder: db.binder.findUnique({ where: { userId } }), | |
anyAdmin0rgMemberships: (await db.orgMember.findFirst({ where: { userId, role: "ADMIN" } })) != null, | |
anyNonAdmin0rgMemberships: (await db.orgMember.findFirst({ where: { userId, NOT: { role: "ADMIN" } } })) != null, | |
anyBinderMemberships: (await db.binderMember.findFirst({ where: { userId } })) != null, | |
}) | |
let userClassifications = await getUserClassifications( | |
user, personalBinder, anyAdminOrgMemberships, | |
anyNonAdmin0rgMemberships, binderMemberships, | |
) | |
let resourceTags = getResourceTags(userClassifications) | |
let resources = b.resources.findMany({ where: { tags: { in: resourceTags } } }) | |
let unreadMessageCount = db.messages.$queryRaw(Prisma.sql`...`)[0]?.count ?? 0 | |
let sections = null | |
if (userClassifications.student || userClassification.teacher) { | |
let role = userClassification.teacher ? "TEACHER" : "STUDENT"; | |
sections = db.sectionUser.findMany({ where: { userId, role } }) | |
} | |
let contacts = null | |
if (userClassification.student) { | |
contacts = db.userContact.findMany({ where: { userId } }) | |
} | |
let binderMemberships = null | |
if (userClassification.teacher || userClassification.studentContact) { | |
binderMemberships = db.binderMember.findMany({ where: { userId }, etc }) | |
} | |
return allProps({ | |
userClassification, user, resources, | |
unreadMessageCount, sections, contacts, binderMemberships | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let db | |
async function loader({ request }) { | |
let session = await requireSession(request) | |
let userId = session.userId | |
let $user = db.user.findUnique({ where: { userId } }) | |
let $personalBinder = db.binder.findUnique({ where: { userId } }) | |
let $anyAdminOrgMemberships = db.orgMember.findFirst({ where: { userId, role: "ADMIN" } }).then(x => x != null) | |
let $anyNonAdminOrgMemberships = db.orgMember.findFirst({ where: { userId, NOT: { role: "ADMIN" } } }).then(x => x != null) | |
let $anyBinderMemberships = db.binderMember.findFirst({ where: { userId } }).then(x => x != null) | |
let $userClassifications = (async () => await getUserClassifications( | |
await $user, await $personalBinder, await $anyAdminOrgMemberships, | |
await $anyNonAdminOrgMemberships, await $anyBinderMemberships, | |
)).apply() | |
// OR | |
// let $userClassifications = Promise.all([$user, $personalBinder, $anyAdminOrgMemberships, $anyNonAdminOrgMemberships, $anyBinderMemberships]).then(r => getUserClassifications(...r)) | |
let $resourceTags = $userClassifications.then(getResourceTags) | |
let $resources = $resourceTags.then(resourceTags => db.resources.findMany({ where: { tags: { in: resourceTags } } })) | |
let $unreadMessageCount = db.messages.$queryRaw(Prisma.sql`...`).then(rows => rows[0]?.count ?? 0) | |
let $sections = $userClassifications.then(userClassifications => { | |
if (userClassifications.student || userClassifications.teacher) { | |
let role = userClassifications.teacher ? "TEACHER" : "STUDENT"; | |
return db.sectionUser.findMany({ where: { userId, role } }) | |
} | |
}) | |
let $contacts = $userClassifications.then(userClassifications => { | |
if (userClassifications.student) { | |
return db.userContact.findMany({ where: { userId } }) | |
} | |
}) | |
let $binderMemberships = $userClassifications.then(userClassifications => { | |
if (userClassifications.teacher || userClassifications.studentContact) { | |
return db.binderMember.findMany({ where: { userId }, etc }) | |
} | |
}) | |
return { | |
userClassifications: await $userClassifications, | |
user: await $user, | |
resources: await $resources, | |
unreadMessageCount: await $unreadMessageCount, | |
sections: await $sections, | |
contacts: await $contacts, | |
binderMemberships: await $binderMemberships, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment