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

ar(feat) [DPCP-40]: Parse Services. #31

Merged
merged 23 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
78c7ddc
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 21, 2024
0e3918a
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 22, 2024
86d2901
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 22, 2024
44e934d
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 22, 2024
ac0a26a
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 22, 2024
897a9c6
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 22, 2024
7d6e100
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 23, 2024
8a45455
Merge branch 'dev' of github.com:dreampipcom/hypnos into ar/feat/DPCP…
angeloreale Aug 24, 2024
db7ee21
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 24, 2024
2be6e4d
Merge branch 'dev' of github.com:dreampipcom/hypnos into ar/feat/DPCP…
angeloreale Aug 25, 2024
65d715a
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 25, 2024
86dda3a
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 25, 2024
8e189ff
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 25, 2024
73780e0
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 25, 2024
8a197dd
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 25, 2024
255564c
Merge branch 'dev' into ar/feat/DPCP-40--parse-services
angeloreale Aug 25, 2024
d213a5c
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 25, 2024
755d0e6
Merge branch 'ar/feat/DPCP-40--parse-services' of github.com:dreampip…
angeloreale Aug 25, 2024
ea813cc
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 25, 2024
3aa0aa8
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 25, 2024
e18b055
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 25, 2024
d5470a7
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 25, 2024
17fb024
ar(feat) [DPCP-40]: Parse Services.
angeloreale Aug 25, 2024
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
1 change: 1 addition & 0 deletions .env.local.public
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ NEXUS_BASE_PATH=

# for docker images only
NEXUS_STANDALONE=
NEXUS_STANDALONE_PRISMA_ONLY=

# keep alive secret for private endpoints healthcheck
NEXUS_KEEPALIVE=
Expand Down
15 changes: 12 additions & 3 deletions lib/auth/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
GetPrivateCommonServices,
UpdatePrivateUserAbilities,
GetPrivateCommonAbilities,
GetPrivateAbilities,
} from '@controller';
import { PrivatePrisma } from '@model';

Expand All @@ -35,15 +36,23 @@ export const GetSession = async ({ cookies = '' }) => {
}
};

// to-do: admin sanitizer

// schema sanitizer
const allUsersSideEffects = async ({ user }: any) => {
const services = await GetPrivateCommonServices({});
const abilities = await GetPrivateCommonAbilities({});
const commonServices = services.map((service: any) => service?.id).map((el: any) => el);
const commonAbilities = abilities.map((ability: any) => ability?.id).map((el: any) => el);

const [dpcpAbility] = await GetPrivateAbilities({ type: 'R', target: 'dpcp-vibemodulator', action: 'view-listings' });

await UpdatePrivateUserServices({ user, services: [...commonServices, ...user.servicesIds], upsert: false });
await UpdatePrivateUserAbilities({ user, abilities: [...commonAbilities, ...user.abilitiesIds], upsert: false });
await UpdatePrivateUserAbilities({
user,
abilities: [...commonAbilities, ...user.abilitiesIds, dpcpAbility.id],
upsert: false,
});
};

export const providers: any[] = [
Expand Down Expand Up @@ -126,7 +135,7 @@ export const authConfig = {
return true;
},
async redirect() {
return `${process.env.MAIN_URL}/dash`;
return `${process.env.MAIN_URL}/dash/signin`;
},
// async jwt({ user, token }) {
// if (user) {
Expand Down Expand Up @@ -191,7 +200,7 @@ export const authConfig = {
trustHost: true,
pages: {
signIn: '/dash/signin',
signOut: '/',
signOut: '/dash/signin',
error: '/dash/error', // Error code passed in query string as ?error=
verifyRequest: '/dash/verify', // (used for check email message)
// newUser: '/' // New users will be directed here on first sign in (leave the property out if not of interest)
Expand Down
31 changes: 22 additions & 9 deletions lib/model/interfaces/get-private-abilities.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint @typescript-eslint/no-unused-vars:0 */
// @controller/get-private-services.ts
import { whoAmI } from '@controller';
import { PrivatePrisma } from '@model';
Expand All @@ -7,8 +8,11 @@ const PAGE_SIZE = 100;
const getPrivateAbilities = async ({
id,
name,
locale = 'es',
locale = 'en',
user,
type,
target,
action,
page = 0,
offset = 0,
limit = PAGE_SIZE,
Expand All @@ -18,8 +22,16 @@ const getPrivateAbilities = async ({

const adaptQuery: any = {
where: {
id,
name: { [locale]: name },
OR: [
{
id,
},
{
type,
target,
action,
},
],
},
skip: page * (limit + offset),
take: limit,
Expand All @@ -31,7 +43,7 @@ const getPrivateAbilities = async ({
const supportedQueries: Record<string, any> = {
user: {
query: {
OR: [{ id: { in: loggedUser?.servicesIds }, name: { [locale]: name } }, { userOwner: loggedUser?.id }],
OR: [{ id: { in: loggedUser?.abilities } }, { userOwnerId: loggedUser.id }],
},
},
// group: {
Expand All @@ -49,15 +61,16 @@ const getPrivateAbilities = async ({
return acc;
}, {});

adaptQuery.where = {
...adaptQuery.where,
...query,
};
adaptQuery.where.OR = query?.OR;
} catch (e) {
throw new Error('Code 001: Wrong filter');
throw new Error('Code 000/2: Wrong filter');
}
}

if (!(adaptQuery?.where?.OR?.length > 0)) {
throw new Error('Code 000/1: Malformed request');
}

const response = await PrivatePrisma.abilities.findMany(adaptQuery);

return response;
Expand Down
17 changes: 11 additions & 6 deletions lib/model/interfaces/get-private-common-abilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const getPrivateCommonAbilities = async ({ page = 0, offset = 0, limit = PAGE_SI
// to-do: move, this will be a middleware
const adaptQuery: any = {
where: {
nature: 'COMMON',
OR: [
{
nature: 'COMMON',
},
],
},
skip: page * (limit + offset),
take: limit,
Expand Down Expand Up @@ -37,15 +41,16 @@ const getPrivateCommonAbilities = async ({ page = 0, offset = 0, limit = PAGE_SI
return acc;
}, {});

adaptQuery.where = {
...adaptQuery.where,
...query,
};
adaptQuery.where.OR = query?.OR;
} catch (e) {
throw new Error('Code 001: Wrong filter');
throw new Error('Code 000/2: Wrong filter');
}
}

if (!(adaptQuery?.where?.OR?.length > 0)) {
throw new Error('Code 000/1: Malformed request');
}

const response = await PrivatePrisma.abilities.findMany(adaptQuery);

return response;
Expand Down
17 changes: 11 additions & 6 deletions lib/model/interfaces/get-private-common-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const getPrivateCommonServices = async ({ page = 0, offset = 0, limit = PAGE_SIZ
// to-do: move, this will be a middleware
const adaptQuery: any = {
where: {
nature: 'COMMON',
OR: [
{
nature: 'COMMON',
},
],
},
skip: page * (limit + offset),
take: limit,
Expand Down Expand Up @@ -37,15 +41,16 @@ const getPrivateCommonServices = async ({ page = 0, offset = 0, limit = PAGE_SIZ
return acc;
}, {});

adaptQuery.where = {
...adaptQuery.where,
...query,
};
adaptQuery.where.OR = query?.OR;
} catch (e) {
throw new Error('Code 001: Wrong filter');
throw new Error('Code 000/2: Wrong filter');
}
}

if (!(adaptQuery?.where?.OR?.length > 0)) {
throw new Error('Code 000/1: Malformed request');
}

const response = await PrivatePrisma.services.findMany(adaptQuery);

return response;
Expand Down
40 changes: 27 additions & 13 deletions lib/model/interfaces/get-private-services.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint @typescript-eslint/no-unused-vars:0 */
// @controller/get-private-services.ts
import { PrivatePrisma } from '@model';
import { whoAmI } from '@controller';
import { whoAmI, canI } from '@controller';

const PAGE_SIZE = 100;

const getPrivateServices = async ({
id,
name,
locale = 'es',
user,
locale = 'en',
target,
page = 0,
offset = 0,
limit = PAGE_SIZE,
Expand All @@ -18,8 +19,14 @@ const getPrivateServices = async ({

const adaptQuery: any = {
where: {
id,
name: { [locale]: name },
OR: [
{
id,
},
{
slug: target,
},
],
},
skip: page * (limit + offset),
take: limit,
Expand All @@ -31,7 +38,7 @@ const getPrivateServices = async ({
const supportedQueries: Record<string, any> = {
user: {
query: {
OR: [{ id: { in: loggedUser?.servicesIds }, name: { [locale]: name } }, { userOwner: loggedUser?.id }],
OR: [{ id: { in: loggedUser?.services } }, { userOwnerId: loggedUser.id }],
},
},
// group: {
Expand All @@ -49,18 +56,25 @@ const getPrivateServices = async ({
return acc;
}, {});

adaptQuery.where = {
...adaptQuery.where,
...query,
};
adaptQuery.where.OR = query?.OR;

const response = await PrivatePrisma.services.findMany(adaptQuery);
return response;
} catch (e) {
throw new Error('Code 001: Wrong filter');
throw new Error(`Code 000/2: Wrong filter: ${e}`);
}
}

const response = await PrivatePrisma.services.findMany(adaptQuery);
if (!(adaptQuery?.where?.OR?.length > 0)) {
throw new Error('Code 000/1: Malformed request');
}

return response;
if (await canI({ type: 'R', action: 'view-listings', target, user: loggedUser })) {
const response = await PrivatePrisma.services.findMany(adaptQuery);
return response;
} else {
throw new Error(`Code 001/0: Not authorized.`);
}
};

export default getPrivateServices;
17 changes: 13 additions & 4 deletions lib/model/interfaces/get-public-listings-iface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ const PAGE_SIZE = 100;

const getPublicListings = async ({ page = 0, offset = 0, limit = PAGE_SIZE, filters = [] }: any) => {
const adaptQuery: any = {
where: {
OR: [
{
status: 'ACTIVE',
},
],
},
skip: page * (limit + offset),
take: limit,
cacheStrategy: process.env.NEXUS_STANDALONE !== 'true' ? { ttl: 90, swr: 60 * 60 * 24 * 7 } : undefined,
Expand Down Expand Up @@ -45,14 +52,16 @@ const getPublicListings = async ({ page = 0, offset = 0, limit = PAGE_SIZE, filt
return acc;
}, {});

adaptQuery.where = {
...query,
};
adaptQuery.where.OR = query?.OR;
} catch (e) {
throw new Error('Code 001: Wrong filter');
throw new Error('Code 000/2: Wrong filter');
}
}

if (!(adaptQuery?.where?.OR?.length > 0)) {
throw new Error('Code 000/1: Malformed request');
}

const response = await PublicPrisma.publicListings.findMany(adaptQuery);

return response;
Expand Down
7 changes: 3 additions & 4 deletions lib/model/interfaces/middleware/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import { getSession, GetSession } from '@auth';
import { GetPrivateAbilities } from '@controller';
import { cookies as nextCookies } from 'next/headers';
export const canI = async ({ name, user }: any) => {
export const canI = async ({ type, target, action, user }: any) => {
try {
const ability = await GetPrivateAbilities({ name });
const abilities = await GetPrivateAbilities({ type, target, action });
// to-do add authorization/validation checks
const yes = user?.abilities?.includes(ability[0]?.id);
const yes = abilities?.some((ability) => user?.abilities.includes(ability.id));
// return the capacity
return yes;
} catch (e) {
Expand All @@ -18,7 +18,6 @@ export const whoAmI = async ({ cookies }: any) => {
const cookieString = nextCookies().getAll().toString();
const session = (await getSession()) || (await GetSession({ cookies: cookieString || cookies }));
// to-do add authorization/validation checks
console.log({ cookieString, session });
return session?.user;
} catch (e) {
throw new Error(`Code 007: Can't identify user ${e}`);
Expand Down
4 changes: 2 additions & 2 deletions lib/model/interfaces/update-private-user-abilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PrivatePrisma } from '@model';
const updatePrivateUserAbilities = async ({ upsert = false, user, abilities }: any) => {
try {
// to-do: move this will be a middleware
if (abilities?.length === 0) return new Error('Code 002: Missing data (abilities)');
if (abilities?.length === 0) return new Error('Code 002/1: Missing data (abilities)');

const loggedUser = user || (await whoAmI({}));

Expand All @@ -29,7 +29,7 @@ const updatePrivateUserAbilities = async ({ upsert = false, user, abilities }: a

return response;
} catch (e) {
throw new Error(`Code 003: Missing results: ${e}`);
throw new Error(`Code 002/0: Missing results: ${e}`);
}
};

Expand Down
12 changes: 5 additions & 7 deletions lib/model/interfaces/update-private-user-favorite-listings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import { whoAmI, canI } from '@controller';
import { PrivatePrisma } from '@model';

const updatePrivateUserFavoriteListings = async ({ upsert = true, user, listings = [], type = 'id' }: any) => {
const updatePrivateUserFavoriteListings = async ({ upsert = true, user, target, listings = [], type = 'id' }: any) => {
try {
if (listings?.length === 0) return new Error('Code 002: Missing data (listings)');

const loggedUser = user || (await whoAmI({}));

console.log({ user, loggedUser });

const delta = upsert ? listings.filter((listing: any) => !loggedUser?.favorites?.includes(listing)) : [];

if ((await canI({ name: 'Ability 1', user: loggedUser })) && type === 'id') {
if ((await canI({ type: 'U', action: 'favorite', target, user: loggedUser })) && type === 'id') {
const swapUserData = loggedUser.favorites.filter((listing: string) => !listings.includes(listing));
const payload = upsert
? {
Expand All @@ -31,7 +29,7 @@ const updatePrivateUserFavoriteListings = async ({ upsert = true, user, listings

const response = await PrivatePrisma.user.update(adaptQuery);
return response;
} else if ((await canI({ name: 'Ability 1', user: loggedUser })) && type === 'string') {
} else if ((await canI({ type: 'U', action: 'favorite', target, user: loggedUser })) && type === 'string') {
const userData = loggedUser?.favoritesStrings;
const swapData = userData.filter((listing: string) => listings.includes(listing));
const transaction = userData
Expand All @@ -54,10 +52,10 @@ const updatePrivateUserFavoriteListings = async ({ upsert = true, user, listings
const response = await PrivatePrisma.user.update(adaptQuery);
return response;
} else {
throw new Error(`403: Not authorized.`);
throw new Error(`Code 001/0: Not authorized.`);
}
} catch (e) {
throw new Error(`Code 003: Missing results: ${e}`);
throw new Error(`Code 002/0: Missing results: ${e}`);
}
};

Expand Down
Loading
Loading