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

feat(envited.ascs.digital): company profile get action #77

Merged
merged 6 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
14 changes: 14 additions & 0 deletions apps/envited.ascs.digital/app/companies/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getProfile } from '../../../common/serverActions'
import { Header } from '../../../modules/Header'

export default async function Index({ params }: { params: { slug: string } }) {
royscheeren marked this conversation as resolved.
Show resolved Hide resolved
const { slug } = params
const profile = await getProfile(slug)

return (
<>
<Header />
<main>{JSON.stringify(profile)}</main>
</>
)
}
1 change: 1 addition & 0 deletions apps/envited.ascs.digital/common/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { RESPONSES } from './response'
export { NAVIGATION_DASHBOARD } from './navigations'

export const MINIMUM_PROFILE_REQUIREMENTS = ['name', 'category', 'logo', 'description']
export const RESTRICTED_PROFILE_FIELDS = ['principalName', 'principalPhone', 'principalEmail']
3 changes: 3 additions & 0 deletions apps/envited.ascs.digital/common/database/queries/profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ export const maybeUpdatePublishedState = (db: DatabaseConnection) => async (data

return db.update(profile).set({ isPublished, updatedAt: new Date() }).where(eq(profile.name, data.name)).returning()
}

export const getProfileBySlug = (db: DatabaseConnection) => async (slug: string) =>
db.select().from(profile).where(eq(profile.slug, slug))
13 changes: 11 additions & 2 deletions apps/envited.ascs.digital/common/database/queries/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import { fromPairs, map, pipe, toPairs } from 'ramda'
import { connectDb } from '../database'
import * as schema from '../schema'
import { fetchTables } from './common'
import { maybeUpdatePublishedState, update as updateProfile } from './profiles'
import { deleteUserById, getUserById, getUserWithProfileById, getUsersByIssuerId, insertUserTx } from './users'
import { getProfileBySlug, maybeUpdatePublishedState, update as updateProfile } from './profiles'
import {
deleteUserById,
getUserById,
getUserByIssuerId,
getUserWithProfileById,
getUsersByIssuerId,
insertUserTx,
} from './users'

const queries = {
deleteUserById,
Expand All @@ -17,6 +24,8 @@ const queries = {
insertUserTx,
updateProfile,
maybeUpdatePublishedState,
getProfileBySlug,
getUserByIssuerId,
}

export const init =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@ describe('common/database/users', () => {

const result = await SUT.insertCompanyProfileTx(tx)({
name: 'NAME',
slug: 'name',
isPublished: false,
})

expect(tx.insert).toHaveBeenCalledWith(profile)
expect(tx.insert().values).toHaveBeenCalledWith({
name: 'NAME',
slug: 'name',
isPublished: false,
createdAt: new Date(),
updatedAt: new Date(),
Expand Down
5 changes: 5 additions & 0 deletions apps/envited.ascs.digital/common/database/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import postgres from 'postgres'
import { isEmpty, prop, propOr } from 'ramda'

import { Profile } from '../../types'
import { slugify } from '../../utils/utils'
royscheeren marked this conversation as resolved.
Show resolved Hide resolved
import * as schema from '../schema'
import {
addressType,
Expand All @@ -32,6 +33,9 @@ export const getUserById = (db: DatabaseConnection) => async (id: string) =>
export const getUserWithProfileById = (db: DatabaseConnection) => async (id: string) =>
db.select().from(user).where(eq(user.id, id)).leftJoin(profile, eq(user.name, profile.name))

export const getUserByIssuerId = (db: DatabaseConnection) => async (issuerId: string) =>
db.select().from(user).where(eq(user.id, issuerId))

export const getUsersByIssuerId = (db: DatabaseConnection) => async (issuerId: string) =>
db.select().from(user).where(eq(user.issuerId, issuerId))

Expand Down Expand Up @@ -200,6 +204,7 @@ export const _txn =

await insertCompanyProfileTx(tx)({
name: credentialSubject.name,
slug: slugify(credentialSubject.name),
streetAddress: credentialSubject.address.streetAddress,
postalCode: credentialSubject.address.postalCode,
addressLocality: credentialSubject.address.addressLocality,
Expand Down
11 changes: 7 additions & 4 deletions apps/envited.ascs.digital/common/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,19 @@ export const profile = pgTable('profile', {
.unique()
.notNull()
.references(() => user.name),
slug: text('slug').unique().notNull(),
description: text('description'),
logo: text('logo'),
streetAddress: text('street_address'),
postalCode: text('postal_code'),
addressLocality: text('address_locality'),
addressCountry: text('address_country'),
firstName: text('first_name'),
lastName: text('last_name'),
phone: text('phone'),
email: text('email'),
salesName: text('sales_name'),
salesPhone: text('sales_phone'),
salesEmail: text('sales_email'),
principalName: text('principal_name'),
principalPhone: text('principal_phone'),
principalEmail: text('principal_email'),
website: text('website'),
offerings: jsonb('offerings'),
isPublished: boolean('is_published').default(false),
Expand Down
21 changes: 21 additions & 0 deletions apps/envited.ascs.digital/common/guards/guards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,25 @@ describe('common/guards', () => {
expect(result).toEqual(true)
})
})

describe('isUsersCompanyProfile', () => {
it('should check if the requested profile is the profile of the logged in users company', () => {
// when ... we want to check if the requested profile is owned by the logged in users company
// then ... we should get the result as expected
const principal = {
id: 'PKH',
name: 'NAME',
profile: {
name: 'NAME',
},
} as any

const profile = {
name: 'NAME',
} as any
const result = SUT.isUsersCompanyProfile(principal)(profile)

expect(result).toEqual(true)
})
})
})
8 changes: 5 additions & 3 deletions apps/envited.ascs.digital/common/guards/guards.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { equals, path, pathOr, prop } from 'ramda'
import { equals, pathOr, prop } from 'ramda'

import { Role, Session, User } from '../../common/types/types'

Expand All @@ -12,5 +12,7 @@ export const isOwnUser = (user: User) => (session: Session) =>
export const userIsIssuedByLoggedInUser = (user: User) => (session: Session) =>
equals(prop('issuerId')(user))(pathOr('', ['user', 'pkh'])(session))

export const isOwnProfile = (user: User) => (profile: { name?: string }) =>
equals(path(['profile', 'name'])(user))(prop('name')(profile))
export const isOwnProfile = (user: User) => (profile: { name: string }) =>
equals(prop('name')(user))(prop('name')(profile))

export const isUsersCompanyProfile = isOwnProfile
9 changes: 8 additions & 1 deletion apps/envited.ascs.digital/common/guards/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export { isFederator, isOwnUser, isOwnProfile, isPrincipal, userIsIssuedByLoggedInUser } from './guards'
export {
isFederator,
isOwnUser,
isOwnProfile,
isPrincipal,
userIsIssuedByLoggedInUser,
isUsersCompanyProfile,
} from './guards'
2 changes: 1 addition & 1 deletion apps/envited.ascs.digital/common/serverActions/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { getUserById, getUsersByIssuerId, insertUser } from './users'
export { updateProfile } from './profiles'
export { updateProfile, getProfile } from './profiles'
176 changes: 176 additions & 0 deletions apps/envited.ascs.digital/common/serverActions/profiles/get.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import { Role } from '../../types'
import * as SUT from './get'

describe('serverActions/profiles/get', () => {
describe('get', () => {
it('should get the own full profile as expected', async () => {
// when ... we want to get the full profile for a principal user
// then ... it should get the profile as expected
const getServerSessionStub = jest.fn().mockResolvedValue({
user: {
id: 'USER_PRINCIPAL_PKH',
role: Role.principal,
},
})

const dbStub = jest.fn().mockResolvedValue({
getProfileBySlug: jest.fn().mockResolvedValue([
{
name: 'USER_PRINCIPAL_NAME',
description: 'USER_DESCRIPTION',
principalName: 'USER_PRINCIPAL_NAME',
},
]),
getUserById: jest.fn().mockResolvedValue([
{
name: 'USER_PRINCIPAL_NAME',
},
]),
getUserByIssuerId: jest.fn().mockResolvedValue([
{
name: 'USER_PRINCIPAL_NAME',
},
]),
})

const slug = 'PROFILE_SLUG'

const result = await SUT._get({ db: dbStub, getServerSession: getServerSessionStub })(slug)
const db = await dbStub()
expect(result).toEqual({
name: 'USER_PRINCIPAL_NAME',
description: 'USER_DESCRIPTION',
principalName: 'USER_PRINCIPAL_NAME',
})
expect(getServerSessionStub).toHaveBeenCalledWith()
expect(db.getProfileBySlug).toHaveBeenCalledWith(slug)
expect(db.getUserById).toHaveBeenCalledWith('USER_PRINCIPAL_PKH')
expect(db.getUserByIssuerId).not.toHaveBeenCalled()
})

it('should get the full profile for a principals user as expected', async () => {
// when ... we want to get the full profile for the user of a certain principal
// then ... it should get the profile as expected
const getServerSessionStub = jest.fn().mockResolvedValue({
user: {
id: 'USER_PKH',
role: Role.user,
},
})

const dbStub = jest.fn().mockResolvedValue({
getProfileBySlug: jest.fn().mockResolvedValue([
{
name: 'USER_PRINCIPAL_NAME',
description: 'USER_DESCRIPTION',
principalName: 'USER_PRINCIPAL_NAME',
},
]),
getUserById: jest.fn().mockResolvedValue([
{
name: 'USER_NAME',
},
]),
getUserByIssuerId: jest.fn().mockResolvedValue([
{
name: 'USER_PRINCIPAL_NAME',
},
]),
})

const slug = 'PROFILE_SLUG'

const result = await SUT._get({ db: dbStub, getServerSession: getServerSessionStub })(slug)
const db = await dbStub()
expect(result).toEqual({
name: 'USER_PRINCIPAL_NAME',
description: 'USER_DESCRIPTION',
principalName: 'USER_PRINCIPAL_NAME',
})
expect(getServerSessionStub).toHaveBeenCalledWith()
expect(db.getProfileBySlug).toHaveBeenCalledWith(slug)
expect(db.getUserById).toHaveBeenCalledWith('USER_PKH')
expect(db.getUserByIssuerId).toHaveBeenCalled()
})

it('should return a limited profile when there is no session', async () => {
// when ... we want to get the limited profile for a principal user
// then ... it should get the profile as expected
const getServerSessionStub = jest.fn().mockResolvedValue(null)

const dbStub = jest.fn().mockResolvedValue({
getProfileBySlug: jest.fn().mockResolvedValue([
{
name: 'USER_NAME',
description: 'USER_DESCRIPTION',
principalName: 'USER_PRINCIPAL_NAME',
},
]),
getUserById: jest.fn().mockResolvedValue([
{
name: 'USER_NAME',
profile: {
name: 'USER_NAME',
},
},
]),
})

const slug = 'PROFILE_SLUG'

const result = await SUT._get({ db: dbStub, getServerSession: getServerSessionStub })(slug)
const db = await dbStub()
expect(result).toEqual({
name: 'USER_NAME',
description: 'USER_DESCRIPTION',
})
expect(getServerSessionStub).toHaveBeenCalledWith()
expect(db.getProfileBySlug).toHaveBeenCalledWith(slug)
expect(db.getUserById).not.toHaveBeenCalled()
})

it('should throw an error when the profile id is missing', async () => {
// when ... we want to get a profile but the id is missing
// then ... it should throw an error
const getServerSessionStub = jest.fn().mockResolvedValue([
{
user: {
id: 'USER_PRINCIPAL_PKH',
role: Role.principal,
},
},
])

const dbStub = jest.fn().mockResolvedValue({})

const slug = ''

expect.assertions(3)
await expect(() => SUT._get({ db: dbStub, getServerSession: getServerSessionStub })(slug)).rejects.toThrow()
expect(getServerSessionStub).not.toHaveBeenCalledWith()
expect(dbStub).not.toHaveBeenCalled()
})
})

it('should throw an error when the profile is not found', async () => {
// when ... we want to get a non existant profile
// then ... it should throw an error
const getServerSessionStub = jest.fn().mockResolvedValue({
user: {
id: 'USER_PRINCIPAL_PKH',
role: Role.principal,
},
})

const dbStub = jest.fn().mockResolvedValue({
getProfileBySlug: jest.fn().mockResolvedValue(null),
})

const slug = 'NON_EXISTANT_PROFILE_SLUG'
const db = await dbStub()
await expect(() => SUT._get({ db: dbStub, getServerSession: getServerSessionStub })(slug)).rejects.toThrow()
expect(getServerSessionStub).toHaveBeenCalledWith()
expect(dbStub).toHaveBeenCalledWith()
expect(db.getProfileBySlug).toHaveBeenCalledWith(slug)
})
})
Loading
Loading