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

Member identities optimizations #2553

Merged
merged 9 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
33 changes: 33 additions & 0 deletions backend/src/api/member/identity/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { safeWrap } from '@/middlewares/errorMiddleware'

export default (app) => {
// Member Identity List
app.get(
`/tenant/:tenantId/member/:memberId/identity`,
safeWrap(require('./memberIdentityList').default),
)

// Member Identity Create
app.post(
`/tenant/:tenantId/member/:memberId/identity`,
safeWrap(require('./memberIdentityCreate').default),
)

// Member Identity Create Multiple
app.put(
`/tenant/:tenantId/member/:memberId/identity`,
safeWrap(require('./memberIdentityCreateMultiple').default),
)

// Member Identity Update
app.patch(
`/tenant/:tenantId/member/:memberId/identity/:id`,
safeWrap(require('./memberIdentityUpdate').default),
)

// Member Identity Delete
app.delete(
`/tenant/:tenantId/member/:memberId/identity/:id`,
safeWrap(require('./memberIdentityDelete').default),
)
}
30 changes: 30 additions & 0 deletions backend/src/api/member/identity/memberIdentityCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Permissions from '../../../security/permissions'
import PermissionChecker from '../../../services/user/permissionChecker'
import MemberIdentityService from '@/services/member/memberIdentityService'

/**
* POST /tenant/{tenantId}/member/:memberId/identity
* @summary Create member identity
* @tag Members
* @security Bearer
* @description Create one member identity.
* @pathParam {string} tenantId - Your workspace/tenant ID | {string} memberId - member ID
* @response 200 - Ok
* @responseContent {MemberList} 200.application/json
* @responseExample {MemberList} 200.application/json.MemberIdentity
* @response 401 - Unauthorized
* @response 429 - Too many requests
*/
export default async (req, res) => {
new PermissionChecker(req).validateHas(Permissions.values.memberEdit)

const memberIdentityService = new MemberIdentityService(req)

const payload = await memberIdentityService.create(
req.params.tenantId,
req.params.memberId,
req.body,
)

await req.responseHandler.success(req, res, payload)
}
30 changes: 30 additions & 0 deletions backend/src/api/member/identity/memberIdentityCreateMultiple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Permissions from '../../../security/permissions'
import PermissionChecker from '../../../services/user/permissionChecker'
import MemberIdentityService from '@/services/member/memberIdentityService'

/**
* PUT /tenant/{tenantId}/member/:memberId/identity
* @summary Create member identities
* @tag Members
* @security Bearer
* @description Create multiple member identity.
* @pathParam {string} tenantId - Your workspace/tenant ID | {string} memberId - member ID
* @response 200 - Ok
* @responseContent {MemberList} 200.application/json
* @responseExample {MemberList} 200.application/json.MemberIdentity
* @response 401 - Unauthorized
* @response 429 - Too many requests
*/
export default async (req, res) => {
new PermissionChecker(req).validateHas(Permissions.values.memberEdit)

const memberIdentityService = new MemberIdentityService(req)

const payload = await memberIdentityService.createMultiple(
req.params.tenantId,
req.params.memberId,
req.body.identities,
)

await req.responseHandler.success(req, res, payload)
}
27 changes: 27 additions & 0 deletions backend/src/api/member/identity/memberIdentityDelete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Permissions from '../../../security/permissions'
import PermissionChecker from '../../../services/user/permissionChecker'
import MemberIdentityService from '@/services/member/memberIdentityService'

/**
* DELETE /tenant/{tenantId}/member/:memberId/identity/:identityId
* @summary Remove member identity
* @tag Members
* @security Bearer
* @description Remove member identity.
* @pathParam {string} tenantId - Your workspace/tenant ID | {string} memberId - member ID | {string} identityId - member identity ID
* @response 200 - Ok
* @responseContent {MemberList} 200.application/json
* @responseExample {MemberList} 200.application/json.MemberIdentity
* @response 401 - Unauthorized
* @response 404 - Not found
* @response 429 - Too many requests
*/
export default async (req, res) => {
new PermissionChecker(req).validateHas(Permissions.values.memberEdit)

const memberIdentityService = new MemberIdentityService(req)

const payload = await memberIdentityService.delete(req.params.id, req.params.memberId)

await req.responseHandler.success(req, res, payload)
}
27 changes: 27 additions & 0 deletions backend/src/api/member/identity/memberIdentityList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Permissions from '../../../security/permissions'
import PermissionChecker from '../../../services/user/permissionChecker'
import MemberIdentityService from '@/services/member/memberIdentityService'

/**
* GET /tenant/{tenantId}/member/:memberId/identity
* @summary Query member identities
* @tag Members
* @security Bearer
* @description Query member identities.
* @pathParam {string} tenantId - Your workspace/tenant ID | {string} memberId - member ID
* @response 200 - Ok
* @responseContent {MemberList} 200.application/json
* @responseExample {MemberList} 200.application/json.Member
* @response 401 - Unauthorized
* @response 404 - Not found
* @response 429 - Too many requests
*/
export default async (req, res) => {
new PermissionChecker(req).validateHas(Permissions.values.memberRead)

const memberIdentityService = new MemberIdentityService(req)

const payload = await memberIdentityService.list(req.params.memberId)

await req.responseHandler.success(req, res, payload)
}
27 changes: 27 additions & 0 deletions backend/src/api/member/identity/memberIdentityUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Permissions from '../../../security/permissions'
import PermissionChecker from '../../../services/user/permissionChecker'
import MemberIdentityService from '@/services/member/memberIdentityService'

/**
* PATCH /tenant/{tenantId}/member/:memberId/identity/:identityId
* @summary Update member identity
* @tag Members
* @security Bearer
* @description Update member identity.
* @pathParam {string} tenantId - Your workspace/tenant ID | {string} memberId - member ID | {string} identityId - member identity ID
* @response 200 - Ok
* @responseContent {MemberList} 200.application/json
* @responseExample {MemberList} 200.application/json.MemberIdentity
* @response 401 - Unauthorized
* @response 404 - Not found
* @response 429 - Too many requests
*/
export default async (req, res) => {
new PermissionChecker(req).validateHas(Permissions.values.memberEdit)

const memberIdentityService = new MemberIdentityService(req)

const payload = await memberIdentityService.update(req.params.id, req.params.memberId, req.body)

await req.responseHandler.success(req, res, payload)
}
2 changes: 2 additions & 0 deletions backend/src/api/member/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ export default (app) => {
safeWrap(require('./memberNotMerge').default),
)
app.patch(`/tenant/:tenantId/member`, safeWrap(require('./memberUpdateBulk').default))

require('./identity').default(app)
}
2 changes: 1 addition & 1 deletion backend/src/api/member/memberFind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default async (req, res) => {
}
}

const payload = await new MemberService(req).findById(req.params.id, segmentId)
const payload = await new MemberService(req).findById(req.params.id, segmentId, req.query.include)

await req.responseHandler.success(req, res, payload)
}
21 changes: 4 additions & 17 deletions backend/src/api/member/memberUnmergePreview.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
import { IMemberIdentity, MemberIdentityType } from '@crowd/types'
import Permissions from '../../security/permissions'
import MemberService from '../../services/memberService'
import PermissionChecker from '../../services/user/permissionChecker'

export default async (req, res) => {
new PermissionChecker(req).validateHas(Permissions.values.memberEdit)

const body = req.body

const identity: IMemberIdentity = {
sourceId: body.sourceId,
platform: body.platform,
value: body.value ? body.value : body.username,
type: body.type ? body.type : MemberIdentityType.USERNAME,
verified: body.verified !== undefined ? body.verified : true,
tenantId: body.tenantId,
integrationId: body.integrationId,
memberId: body.memberId,
createdAt: body.createdAt,
updatedAt: body.updatedAt,
}

const payload = await new MemberService(req).unmergePreview(req.params.memberId, identity)
const payload = await new MemberService(req).unmergePreview(
req.params.memberId,
req.body.identity,
)

await req.responseHandler.success(req, res, payload, 200)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE "memberIdentities" DROP COLUMN "id";
ALTER TABLE "memberIdentities" DROP CONSTRAINT "memberIdentities_pkey";
ALTER TABLE "memberIdentities" ADD PRIMARY KEY ("memberId", platform, username);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE "memberIdentities" ADD COLUMN "id" UUID NOT NULL DEFAULT uuid_generate_v4();
ALTER TABLE "memberIdentities" DROP CONSTRAINT "memberIdentities_pkey";
ALTER TABLE "memberIdentities" ADD PRIMARY KEY (id);
7 changes: 4 additions & 3 deletions backend/src/database/models/memberIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { DataTypes } from 'sequelize'

export default (sequelize) => {
const memberIdentity = sequelize.define('memberIdentity', {
memberId: {
id: {
type: DataTypes.UUID,
primaryKey: true,
},
memberId: {
type: DataTypes.UUID,
},
platform: {
type: DataTypes.TEXT,
primaryKey: true,
},
value: {
type: DataTypes.TEXT,
primaryKey: true,
},
type: {
type: DataTypes.TEXT,
Expand Down
136 changes: 136 additions & 0 deletions backend/src/database/repositories/member/memberIdentityRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { IMemberIdentity } from '@crowd/types'
import {
checkIdentityExistance,
createMemberIdentity,
deleteMemberIdentity,
fetchMemberIdentities,
findMemberIdentityById,
updateMemberIdentity,
} from '@crowd/data-access-layer/src/members'
import { Error409 } from '@crowd/common'
import { IRepositoryOptions } from '../IRepositoryOptions'
import SequelizeRepository from '../sequelizeRepository'

class MemberIdentityRepository {
static async list(memberId: string, options: IRepositoryOptions) {
const transaction = SequelizeRepository.getTransaction(options)
const qx = SequelizeRepository.getQueryExecutor(options, transaction)

// List all member identities
return fetchMemberIdentities(qx, memberId)
}

static async create(
tenantId: string,
memberId: string,
data: Partial<IMemberIdentity>,
options: IRepositoryOptions,
) {
const transaction = SequelizeRepository.getTransaction(options)
const qx = SequelizeRepository.getQueryExecutor(options, transaction)

// Check if identity already exists
const existingIdentities = await checkIdentityExistance(qx, data.value, data.platform)
if (existingIdentities.length > 0) {
throw new Error409(
options.language,
'errors.alreadyExists',
// @ts-ignore
JSON.stringify({
memberId: existingIdentities[0].memberId,
}),
)
}

// Create member identity
await createMemberIdentity(qx, tenantId, memberId, data)

// List all member identities
return fetchMemberIdentities(qx, memberId)
}

static async findById(memberId: string, id: string, options: IRepositoryOptions) {
const transaction = SequelizeRepository.getTransaction(options)
const qx = SequelizeRepository.getQueryExecutor(options, transaction)

// Create member identity
return findMemberIdentityById(qx, memberId, id)
}

static async createMultiple(
tenantId: string,
memberId: string,
data: Partial<IMemberIdentity>[],
options: IRepositoryOptions,
) {
const transaction = SequelizeRepository.getTransaction(options)
const qx = SequelizeRepository.getQueryExecutor(options, transaction)

// Check if any of the identities already exist
for (const identity of data) {
const existingIdentities = await checkIdentityExistance(qx, identity.value, identity.platform)

if (existingIdentities.length > 0) {
throw new Error409(
options.language,
'errors.alreadyExists',
// @ts-ignore
JSON.stringify({
memberId: existingIdentities[0].memberId,
}),
)
}
}

// Create member identities
for (const identity of data) {
await createMemberIdentity(qx, tenantId, memberId, identity)
}

// List all member identities
return fetchMemberIdentities(qx, memberId)
}

static async update(
id: string,
memberId: string,
data: Partial<IMemberIdentity>,
options: IRepositoryOptions,
) {
const transaction = SequelizeRepository.getTransaction(options)
const qx = SequelizeRepository.getQueryExecutor(options, transaction)

// Check if identity already exists
const existingIdentities = await checkIdentityExistance(qx, data.value, data.platform)
const filteredExistingIdentities = existingIdentities.filter((i) => i.id !== id)
if (filteredExistingIdentities.length > 0) {
throw new Error409(
options.language,
'errors.alreadyExists',
// @ts-ignore
JSON.stringify({
memberId: filteredExistingIdentities[0].memberId,
}),
)
}

// Update member identity with new data
await updateMemberIdentity(qx, memberId, id, data)

// List all member identities
return fetchMemberIdentities(qx, memberId)
}

static async delete(id: string, memberId: string, options: IRepositoryOptions) {
const transaction = SequelizeRepository.getTransaction(options)
const qx = SequelizeRepository.getQueryExecutor(options, transaction)

// Delete member identity
await deleteMemberIdentity(qx, memberId, id)

// List all member identities
return fetchMemberIdentities(qx, memberId)
}
}

export default MemberIdentityRepository
Loading
Loading