Public
Edited
Nov 23, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
currentUser = entries[currentUserId]
Insert cell
currentUserLanguages = currentUser ? languagesByUserId[currentUser.sys.id] || [] : []
Insert cell
currentUserOrgs = currentUser ? orgsByUserId[currentUser.sys.id] || [] : []
Insert cell
currentUserBookings = _.uniqBy(
[...currentUserOrgMemberBookings, ...currentUserParticipantBookings],
"sys.id"
)
Insert cell
currentUserOrgMemberBookings = currentUser ? bookingsByOrgUserId[currentUser.sys.id] || [] : []
Insert cell
currentUserParticipantBookings = currentUser ? bookingsByParticipantUserId[currentUser.sys.id] || [] : []
Insert cell
Insert cell
Insert cell
Insert cell
currentOrg = orgsBySlug[currentOrgSlug]
Insert cell
currentOrgBookings = currentOrg ? bookingsByOrgId[currentOrg.sys.id] || [] : []
Insert cell
currentOrgBookingTypes = currentOrg ? bookingTypesByOrgId[currentOrg.sys.id] || [] : []
Insert cell
currentOrgUsers = currentOrg ? usersByOrgId[currentOrg.sys.id] || [] : []
Insert cell
Insert cell
Insert cell
currentBookingType = currentOrgBookingTypes[currentBookingTypeIndex]
Insert cell
currentBookingTypePool = getParticipantPool(currentBookingType)
Insert cell
Insert cell
Insert cell
currentBooking = currentOrgBookings[currentBookingIndex]
Insert cell
currentBooking ? get(currentBooking.fields.bookingType) : undefined
Insert cell
currentBookingLanguages = currentBooking ? languagesByBookingId[currentBooking.sys.id] || [] : []
Insert cell
currentBookingParticipants = currentBooking ? participantsByBookingId[currentBooking.sys.id] || [] : []
Insert cell
currentBookingOrgUsers = currentBooking ? orgUsersByBookingId[currentBooking.sys.id] || [] : []
Insert cell
Insert cell
Insert cell
bookingsByOrgId = _.groupBy(bookings, (booking) => {
return booking.fields.org.sys.id
})
Insert cell
bookingsByBookingTypeId = _.groupBy(bookings, (booking) => {
return booking.fields.bookingType.sys.id
})
Insert cell
bookingsByOrgUserId = groupByMultiple(bookings, (booking) => {
return _.map(booking.fields.orgUsers, 'sys.id')
})
Insert cell
bookingsByParticipantUserId = groupByMultiple(bookings, (booking) => {
return _.map(booking.fields.participants, 'sys.id')
})
Insert cell
Insert cell
bookingTypesByOrgId = _.groupBy(bookingTypes, (bookingType) => {
return bookingType.fields.org.sys.id
})
Insert cell
Insert cell
languagesBySlug = _.keyBy(languages, 'fields.slug')
Insert cell
languagesByUserId = _.mapValues(_.keyBy(users, 'sys.id'), (user) => {
return get(user.fields.languages)
})
Insert cell
languagesByBookingId = _.mapValues(_.keyBy(bookings, "sys.id"), (booking) => {
return getSharedLanguages([...booking.fields.orgUsers, ...booking.fields.participants]);
})
Insert cell
Insert cell
orgsBySlug = _.keyBy(orgs, 'fields.slug')
Insert cell
Insert cell
Insert cell
platformsByUserId = _.mapValues(_.keyBy(users, 'sys.id'), (user) => {
return get(user.fields.platforms)
})
Insert cell
Insert cell
targetGroupsByUserId = _.mapValues(_.keyBy(users, 'sys.id'), (user) => {
return get(user.fields.targetGroups)
})
Insert cell
Insert cell
participantsByBookingId = _.mapValues(_.keyBy(bookings, 'sys.id'), (booking) => {
return get(booking.fields.participants)
})
Insert cell
orgUsersByBookingId = _.mapValues(_.keyBy(bookings, 'sys.id'), (booking) => {
return get(booking.fields.orgUsers)
})
Insert cell
usersByEmail = _.keyBy(users, 'fields.email')
Insert cell
usersByOrgId = _.mapValues(_.keyBy(orgs, 'sys.id'), (org) => {
return get(org.fields.users) || []
})
Insert cell
Insert cell
bookings = _.orderBy(entriesByType.booking || [], 'fields.scheduledFor')
Insert cell
bookingTypes = entriesByType.bookingType || []
Insert cell
languages = _.orderBy(entriesByType.language || [], 'fields.slug')
Insert cell
orgs = _.orderBy(entriesByType.org || [], 'fields.name')
Insert cell
platforms = entriesByType.platform || []
Insert cell
targetGroups = entriesByType.targetGroup || []
Insert cell
users = _.orderBy(entriesByType.user || [], 'fields.email')
Insert cell
Insert cell
function getSharedLanguages(...users) {
return getShared(languagesByUserId, ...users);
}
Insert cell
function getShared (listByEntry, ...objects) {
const perEntry = _.map(_.flattenDeep(objects), (obj) => {
return listByEntry[obj.sys.id];
});

return _.intersectionBy(...perEntry, "sys.id");

}
Insert cell
function getParticipantPool(bookingType) {
// Filter out users who...
if (bookingType) {
return _.filter(users, (user) => {
// 1. Are in the same organisation
if (
user.fields.orgs &&
user.fields.orgs.length &&
_.find(user.fields.orgs, ["sys.id", bookingType.fields.org.sys.id])
) {
return false;
}

// 2. Are not on accepted platforms (if platforms have been set for booking type)
if (bookingType.fields.platforms && bookingType.fields.platforms.length) {
if (
_.intersectionBy(
bookingType.fields.platforms,
user.fields.platforms,
"sys.id"
).length
) {
return false;
}
}

// 3. Are not in at least one target group
if (
bookingType.fields.targetGroups &&
bookingType.fields.targetGroups.length
) {
if (
_.intersectionBy(
bookingType.fields.targetGroups,
user.fields.targetGroups,
"sys.id"
).length
) {
return false;
}
}

// 4. Already have bookings in the booking type
if (
_.find(bookingsByBookingTypeId[bookingType.sys.id], (booking) => {
return _.find(booking.fields.participants, ["sys.id", user.sys.id]);
})
) {
return false;
}

// 5. Do not share any language with required org users
if (
bookingType.fields.orgUsersRequired &&
bookingType.fields.orgUsersRequired.length &&
!getSharedLanguages(...bookingType.fields.orgUsersRequired, user).length
) {
return false;
}

return true;
});
}

return [];
}
Insert cell
Insert cell
Insert cell
function simplify(...args) {
const items = _.flattenDeep(args);

const simplified = items.map((obj) => {
if (obj) {
const f = _.mapValues(obj.fields, (value) => {
if (
_.isArray(value) &&
value.length &&
value[0] &&
value[0].sys &&
value[0].sys.id
) {
return _.map(value, "sys.id");
}
return value;
});

return {
...f,
...obj.sys,
contentType: obj.sys.contentType.sys.id,
environment: obj.sys.contentType.sys.id,
space: obj.sys.space.sys.id,
...(obj.metadata || {})
};
}

return {};
});

if (args.length === 1 && !_.isArray(args[0])) {
return simplified[0];
}

return simplified;
}
Insert cell
Insert cell
Insert cell
Insert cell
fetchEntries(...contentTypes)
Insert cell
fetchUserByEmail('jerryjappinen@lateralnord.com')
Insert cell
async function fetchUserByEmail (email) {
return fetchEntries({
content_type: 'user',
'fields.email': email
})
}
Insert cell
async function fetchOrgBySlug (slug) {
const ids = await fetchEntries({
content_type: 'org',
'fields.slug': slug
})

// Fetch content also, but don't wait
fetchOrgContent(ids[0])

return ids
}
Insert cell
fetchOrgContent('2gRTFFLiXGN6gkvi5jE5DR')
Insert cell
async function fetchOrgContent(orgId) {
return fetchEntries(
{
content_type: "booking",
"fields.org.sys.contentType.sys.id": "org",
"fields.org.sys.id": orgId
},
{
content_type: "bookingType",
"fields.org.sys.contentType.sys.id": "org",
"fields.org.sys.id": orgId
}
);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
contentTypes = [
"booking",
"bookingType",
"language",
"org",
"platform",
"targetGroup",
"user"
]
Insert cell
tableData = simplify(entriesByType[tableDataType] || [])
Insert cell
entriesByType
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { formatKey } from "@jerryjappinen/ui"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more