fix: GoogleCalendar - Ensure that Delegation Credentials are able to reuse access_token (#21389)
Co-authored-by: Omar López <[email protected]>
This commit is contained in:
1 parent
b8ffcef45e
commit
30060f03eb
21 files changed
+1457
-834
No files matched your search
@@ -0,0 +1,141 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* This script is used to prepare local environment for delegation credentials testing.
|
||||
* It prepares Acme organization and its owner user with email [email protected] to test Delegation Credentials with Calendar Cache
|
||||
*/
|
||||
const { PrismaClient } = require("@prisma/client");
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main() {
|
||||
// Parse newEmail from args
|
||||
const newEmail = process.argv[2] || "[email protected]";
|
||||
console.log(`Using newEmail: ${newEmail}`);
|
||||
|
||||
// 1. Update user email
|
||||
let user = await prisma.user.findUnique({
|
||||
where: { email: "[email protected]" },
|
||||
});
|
||||
if (!user) {
|
||||
// Check if user with newEmail exists
|
||||
user = await prisma.user.findUnique({ where: { email: newEmail } });
|
||||
if (user) {
|
||||
console.log(`User with newEmail (${newEmail}) already exists. Skipping email update step.`);
|
||||
} else {
|
||||
console.error(
|
||||
"User with email [email protected] not found, and user with newEmail also not found."
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
} else {
|
||||
if (user.email !== newEmail) {
|
||||
await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: { email: newEmail },
|
||||
});
|
||||
console.log(`Updated user email to ${newEmail}`);
|
||||
} else {
|
||||
console.log("User email already set to newEmail, skipping update.");
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Find organization (Team)
|
||||
const org = await prisma.team.findFirst({
|
||||
where: { slug: "acme", isOrganization: true },
|
||||
});
|
||||
if (!org) {
|
||||
console.error("Organization (Team) with slug=acme and isOrganization=true not found.");
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`Found organization: id=${org.id}, slug=${org.slug}`);
|
||||
|
||||
// 3. Ensure TeamFeatures: delegation-credential
|
||||
const delegationFeature = await prisma.teamFeatures.findUnique({
|
||||
where: {
|
||||
teamId_featureId: {
|
||||
teamId: org.id,
|
||||
featureId: "delegation-credential",
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!delegationFeature) {
|
||||
await prisma.teamFeatures.create({
|
||||
data: {
|
||||
teamId: org.id,
|
||||
featureId: "delegation-credential",
|
||||
assignedAt: new Date(),
|
||||
assignedBy: "prepare-local-script",
|
||||
},
|
||||
});
|
||||
console.log("Created TeamFeatures: delegation-credential");
|
||||
} else {
|
||||
console.log("TeamFeatures: delegation-credential already exists, skipping.");
|
||||
}
|
||||
|
||||
// 4. Ensure TeamFeatures: calendar-cache
|
||||
const calendarCacheFeature = await prisma.teamFeatures.findUnique({
|
||||
where: {
|
||||
teamId_featureId: {
|
||||
teamId: org.id,
|
||||
featureId: "calendar-cache",
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!calendarCacheFeature) {
|
||||
await prisma.teamFeatures.create({
|
||||
data: {
|
||||
teamId: org.id,
|
||||
featureId: "calendar-cache",
|
||||
assignedAt: new Date(),
|
||||
assignedBy: "prepare-local-script",
|
||||
},
|
||||
});
|
||||
console.log("Created TeamFeatures: calendar-cache");
|
||||
} else {
|
||||
console.log("TeamFeatures: calendar-cache already exists, skipping.");
|
||||
}
|
||||
|
||||
// 5. Add WorkspacePlatform record
|
||||
const workspacePlatform = await prisma.workspacePlatform.findUnique({
|
||||
where: { slug: "google" },
|
||||
});
|
||||
if (!workspacePlatform) {
|
||||
await prisma.workspacePlatform.create({
|
||||
data: {
|
||||
slug: "google",
|
||||
name: "Google",
|
||||
enabled: true,
|
||||
description: "Google Workspace Platform",
|
||||
defaultServiceAccountKey: {}, // Empty object, update as needed
|
||||
},
|
||||
});
|
||||
console.log("Created WorkspacePlatform: google");
|
||||
} else {
|
||||
console.log("WorkspacePlatform: google already exists, skipping.");
|
||||
}
|
||||
|
||||
// 6. Enable Feature records for 'calendar-cache' and 'delegation-credential'
|
||||
const featureSlugs = ["calendar-cache", "delegation-credential"];
|
||||
for (const slug of featureSlugs) {
|
||||
const feature = await prisma.feature.findUnique({ where: { slug } });
|
||||
if (!feature) {
|
||||
console.error(`Feature with slug ${slug} not found.`);
|
||||
process.exit(1);
|
||||
}
|
||||
if (!feature.enabled) {
|
||||
await prisma.feature.update({ where: { slug }, data: { enabled: true } });
|
||||
console.log(`Enabled Feature: ${slug}`);
|
||||
} else {
|
||||
console.log(`Feature: ${slug} already enabled, skipping.`);
|
||||
}
|
||||
}
|
||||
console.log(`Now you can sign in with ${newEmail} and create a new Delegation Credential.`);
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in new issue
Block a user