feat: mock exercise template api (#613)
* feat: move mock exercise template to api * feat: add template migration file * chore: - remove exercise templates form api - rework api to pull exercise templates from db - add exercise template seed to seed file - declutter seed file * chore: - refactor template fetch to be dependent on the selected tag - add tag to schema, remote db and template's seed file - clear remote db's template table and replace data and their respective tags * chore: add number of questions and center loading screen on the frontend * chore: pr fixes * chore: type fixes * chore: - move seed functionality to /seed folder - add typing for individual seed functions - pr fixes * chore: pr fixes v2 * update output css * refactor: update seeding script and schema definitions - Renamed `seedTypes` to `seedQuestionTypes` for clarity in the seeding process. - Enhanced the seeding script to include a command-line interface for better usability, allowing specific seed functions to be executed based on flags. - Removed redundant unique constraints in the schema definitions for `courseNewsfeedComment` and `exerciseTemplate` to streamline database structure. * feat: add email verification error handling page and update session management - Introduced a new page for handling email verification errors, providing user-friendly messages based on error types. - Updated session management logic to improve handling of public routes and session states. - Enhanced security policies in the Svelte configuration to include additional sources for styles and scripts. - Removed the old email verification error page to streamline the codebase. * move the exercise template to cloudflare cdn --------- Co-authored-by: Rotimi Best <[email protected]>
This commit is contained in:
co-authored by
Rotimi Best
parent
ac3f35a97f
commit
50d6ee3111
@@ -0,0 +1,69 @@
|
||||
import { account, db } from '@db/drizzle';
|
||||
import { TAccount } from '@db/types';
|
||||
|
||||
interface TUserSeedData extends TAccount {
|
||||
encrypted_password?: string;
|
||||
identities?: Array<{
|
||||
provider: string;
|
||||
identity_data?: {
|
||||
sub?: string;
|
||||
};
|
||||
}>;
|
||||
}
|
||||
|
||||
export async function seedAccount({ usersData }: { usersData: TUserSeedData[] }) {
|
||||
const existingAccounts = await db.select().from(account);
|
||||
const existingAccountKeys = existingAccounts.map((a) => `${a.userId}-${a.providerId}`);
|
||||
|
||||
const accountsToInsert = [];
|
||||
for (const userData of usersData) {
|
||||
// Process identities to create accounts for all users in usersData
|
||||
// (whether they were just inserted or already existed)
|
||||
|
||||
// Process identities to create accounts
|
||||
if (userData.identities && Array.isArray(userData.identities)) {
|
||||
for (const identity of userData.identities) {
|
||||
const accountKey = `${userData.id}-${identity.provider === 'email' ? 'credential' : identity.provider}`;
|
||||
|
||||
if (existingAccountKeys.includes(accountKey)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (identity.provider === 'email') {
|
||||
// Create credential account for email provider
|
||||
accountsToInsert.push({
|
||||
userId: userData.id,
|
||||
providerId: 'credential',
|
||||
accountId: userData.id,
|
||||
password: userData.encrypted_password
|
||||
});
|
||||
} else {
|
||||
// For social providers, use the sub from identity_data
|
||||
accountsToInsert.push({
|
||||
userId: userData.id,
|
||||
providerId: identity.provider,
|
||||
accountId: identity.identity_data?.sub || userData.id
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Fallback: if no identities array, create credential account from email
|
||||
const accountKey = `${userData.id}-credential`;
|
||||
if (!existingAccountKeys.includes(accountKey)) {
|
||||
accountsToInsert.push({
|
||||
userId: userData.id,
|
||||
providerId: 'credential',
|
||||
accountId: userData.id,
|
||||
password: userData.encrypted_password
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (accountsToInsert.length > 0) {
|
||||
await db.insert(account).values(accountsToInsert);
|
||||
console.log(` ✓ Inserted ${accountsToInsert.length} account(s)`);
|
||||
} else {
|
||||
console.log(' ✓ Accounts already exist, skipping');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user