Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DPCP-2] WIP: Org Tenancy Schema #1

Merged
merged 22 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
# temporary
system/
components/
nsx/
nsx/
mock/


12 changes: 10 additions & 2 deletions lib/actions/actions-init.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
// actions-init.tsx
/* eslint-disable react-hooks/exhaustive-deps, react-hooks/rules-of-hooks */
'use client';
import type { ISupportedContexts, IActionBack, IAction, IStatus, ICreateAction, IDispatch, IPayload } from '@types';
import type {
ISupportedContexts,
IActionBack,
IActionDispatch,
IStatus,
ICreateAction,
IDispatch,
IPayload,
} from '@types';

import { createLogMessage, fluxLog as log } from '@log';
import { useState, useEffect, useContext, useRef } from 'react';

export const CreateAction: ICreateAction =
({ action, type, verb, context }: IActionBack) =>
({ cb }: IAction) => {
({ cb }: IActionDispatch) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const noop: IDispatch = async (...payload: IPayload): Promise<void> => {};

Expand Down
28 changes: 28 additions & 0 deletions lib/actions/auth-actions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable react-hooks/exhaustive-deps, react-hooks/rules-of-hooks */
// auth-actions.ts
'use client';
import { BuildAction, CreateAction } from '@actions';

import { AuthContext } from '@state';

/* to-do: chained actions */

/* private */

/* public */

export const ALogin = BuildAction(CreateAction, {
action: 'login',
type: 'auth',
verb: 'load user',
context: AuthContext,
});

export const ALogout = BuildAction(CreateAction, {
action: 'logout',
type: 'auth',
verb: 'unload user',
context: AuthContext,
});

export { ALogin as ALogIn, ALogout as ALogOut };
2 changes: 1 addition & 1 deletion lib/actions/db-users-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AuthContext } from '@state';
/* public */
export const ALoadUserMeta = BuildAction(CreateAction, {
action: 'hydrate',
type: 'users',
type: 'database',
verb: 'load user meta',
context: AuthContext,
});
15 changes: 2 additions & 13 deletions lib/auth/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint no-unreachable:0 */
// constants.ts TS-Doc?
import type { AuthOptions } from 'next-auth';
import GithubProvider from 'next-auth/providers/github';
import EmailProvider from 'next-auth/providers/email';
import InstagramProvider from 'next-auth/providers/instagram';
import { initUser } from '@controller';

export const authOptions: AuthOptions = {
// Configure one or more authentication providers
Expand Down Expand Up @@ -34,18 +34,7 @@ export const authOptions: AuthOptions = {
session: {
strategy: 'jwt',
},
events: {
async signIn({ user, isNewUser }) {
try {
if (isNewUser) {
return await initUser({ email: user.email });
}
return true;
} catch (e) {
console.error(e);
}
},
},
events: {},
callbacks: {
async signIn() {
// extra sign-in checks
Expand Down
49 changes: 49 additions & 0 deletions lib/model/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// constants.ts
let nexusDatabase = 'test';
let orgsDatabase = '';
let usersDatabase = '';
let defaultOrg = '';
let DATABASE_USERS_STRING = '';
let DATABASE_ORGS_STRING = '';
let DEFAULT_ORG = 'community';

/* lean */
if (process.env.NEXUS_MODE !== 'full') {
nexusDatabase = process.env.MONGODB_DATABASE || nexusDatabase;
} else {
/* full-model */
if (process.env.MONGODB_ORGS_DATABASE) {
orgsDatabase = process.env.MONGODB_ORGS_DATABASE;
}

/* users-override (optional) */
if (process.env.MONGODB_USERS_DATABASE) {
usersDatabase = process.env.MONGODB_USERS_DATABASE;
}

if (process.env.MONGODB_DEFAULT_ORG) {
defaultOrg = process.env.MONGODB_DEFAULT_ORG;
}

// if (process.env.MONGODB_BILLING_DATABASE) {
// billingDatabase = process.env.MONGODB_BILLING_DATABASE;
// }

// if (process.env.MONGODB_KRN_DATABASE) {
// krnDatabase = process.env.MONGODB_KRN_DATABASE;
// }
}

/* __backwards-compatible: should fallback to nexus */
DATABASE_USERS_STRING = usersDatabase || nexusDatabase;
DATABASE_ORGS_STRING = orgsDatabase || nexusDatabase;
DEFAULT_ORG = defaultOrg || 'demo';

export {
/* __default__ */
nexusDatabase as DATABASE_STRING,
/* __backwards-compatible */
DATABASE_USERS_STRING,
DATABASE_ORGS_STRING,
DEFAULT_ORG,
};
7 changes: 4 additions & 3 deletions lib/model/decorators/rm-decorator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// rm-decorator.ts input: rm meta; output: decorated rm meta;
'use server';
import type { IDCharacter, INCharacter, UserSchema } from '@types';
import type { IDCharacter, INCharacter, UserDecoration } from '@types';
import { getUserMeta } from '@controller';

/* private */
const decorateCharacter = (character: INCharacter, uMeta: UserSchema): IDCharacter => {
const decorateCharacter = (character: INCharacter, uMeta: UserDecoration): IDCharacter => {
const decd: IDCharacter = { ...character };
decd.favorite = undefined;
if (uMeta?.rickmorty?.favorites?.characters?.includes(character?.id)) decd.favorite = true;
Expand All @@ -14,7 +14,8 @@ const decorateCharacter = (character: INCharacter, uMeta: UserSchema): IDCharact

/* public */
export const decorateRMCharacters = async (characters: INCharacter[], uid: string): Promise<IDCharacter[]> => {
const uMeta: UserSchema = await getUserMeta({ email: uid });
const uMeta: UserDecoration = await getUserMeta({ email: uid });
console.log({ uMeta: uMeta.rickmorty });
const decd: IDCharacter[] = characters.map((char) => decorateCharacter(char, uMeta));
return decd;
};
3 changes: 2 additions & 1 deletion lib/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export {
DATABASE_STRING,
DATABASE_USERS_STRING,
DATABASE_ORGS_STRING,
} from './interfaces';
DEFAULT_ORG,
} from './constants';

/* rm-decorators */
export { decorateRMCharacters } from './decorators';
10 changes: 2 additions & 8 deletions lib/model/interfaces/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
let nexusDatabase = 'test';
let orgsDatabase = '';
let usersDatabase = '';
let defaultOrg = '';
let DATABASE_USERS_STRING = '';
let DATABASE_ORGS_STRING = '';
let DEFAULT_ORG = 'demo';

/* lean */
if (process.env.NEXUS_MODE !== 'full') {
console.log('DEFAULT DB IS', process.env.MONGODB_DATABASE);

nexusDatabase = process.env.MONGODB_DATABASE || nexusDatabase;
} else {
/* full-model */
Expand All @@ -21,10 +21,6 @@ if (process.env.NEXUS_MODE !== 'full') {
usersDatabase = process.env.MONGODB_USERS_DATABASE;
}

if (process.env.MONGODB_DEFAULT_ORG) {
defaultOrg = process.env.MONGODB_DEFAULT_ORG;
}

// if (process.env.MONGODB_BILLING_DATABASE) {
// billingDatabase = process.env.MONGODB_BILLING_DATABASE;
// }
Expand All @@ -37,13 +33,11 @@ if (process.env.NEXUS_MODE !== 'full') {
/* __backwards-compatible: should fallback to nexus */
DATABASE_USERS_STRING = usersDatabase || nexusDatabase;
DATABASE_ORGS_STRING = orgsDatabase || nexusDatabase;
DEFAULT_ORG = defaultOrg || 'demo';

export {
/* __default__ */
nexusDatabase as DATABASE_STRING,
/* __backwards-compatible */
DATABASE_USERS_STRING,
DATABASE_ORGS_STRING,
DEFAULT_ORG,
};
3 changes: 1 addition & 2 deletions lib/model/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/* mdb-ifaces */

/* const */
export { DATABASE_STRING, DATABASE_USERS_STRING, DATABASE_ORGS_STRING } from './constants';

// default methods
export { NexusInterface } from './mdb-init-interface';
Expand All @@ -12,7 +11,7 @@ export { NexusInterface } from './mdb-init-interface';
export { getUserMeta } from './mdb-get-interface';

// write
export { addToFavorites, initUser } from './mdb-update-interface';
export { commitUpdate, initSignUpUser } from './mdb-update-interface';

/* rm */

Expand Down
22 changes: 20 additions & 2 deletions lib/model/interfaces/mdb-get-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,26 @@ import { patience } from './helpers';
import { NexusInterface } from './mdb-init-interface';

/* public */
export const getUserMeta = async ({ email = '', options }: Pick<UserSchema, 'email'> | string) => {
export const getUserMeta = async ({ email }: Pick<UserSchema, 'email'> | string) => {
const Nexus = await NexusInterface;
const user = await Nexus.getUser(email);
if (!Nexus.currentUser) {
await getMyUser({ email });
}
const user = Nexus.currentUser;
console.log({ user: user.rickmorty });
return user;
};

export const getPublicUser = async ({ email = '', options }: Pick<UserSchema, 'email'> | string) => {
const Nexus = await NexusInterface;
const user = await Nexus.getPublicUser(email);
return user;
};

export const getMyUser = async ({ email = '', options }: Pick<UserSchema, 'email'> | string) => {
const Nexus = await NexusInterface;
if (!Nexus.currentUser) {
await Nexus.initUser({ email });
}
return Nexus.currentUser;
};
Loading
Loading