Skip to content

Commit

Permalink
add admin canceled project status
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosQ96 committed Oct 24, 2021
1 parent 02c43c3 commit 08c80e5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
11 changes: 10 additions & 1 deletion entities/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
JoinTable,
BaseEntity,
OneToMany,
Index
Index,
AfterUpdate
} from 'typeorm'

import { Organisation } from './organisation'
Expand All @@ -18,6 +19,7 @@ import { Reaction } from './reaction'
import { Category } from './category'
import { User } from './user'
import { ProjectStatus } from './projectStatus'
import ProjectTracker from '../services/segment/projectTracker'

@Entity()
@ObjectType()
Expand Down Expand Up @@ -154,7 +156,10 @@ class Project extends BaseEntity {
return this.reactions ? this.reactions.length : 0
}

// Status 7 is deleted status
mayUpdateStatus (user: User) {
if (this.statusId == 7) return false

if (this.users.filter(o => o.id === user.id).length > 0) {
return true
} else {
Expand All @@ -174,6 +179,10 @@ class Project extends BaseEntity {
}
}

static notifySegment(project: any, eventName: string) {
new ProjectTracker(project, eventName).track()
}

owner () {
return this.users[0]
}
Expand Down
4 changes: 3 additions & 1 deletion entities/projectStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
Column,
Entity,
BaseEntity,
OneToMany
OneToMany,
Index
} from 'typeorm'
import { Project } from './project'

Expand All @@ -19,6 +20,7 @@ export class ProjectStatus extends BaseEntity{
@Column('text', { unique: true })
symbol: string

@Index()
@Field()
@Column({ nullable: true })
name: string
Expand Down
30 changes: 26 additions & 4 deletions server/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ const AdminBroExpress = require('@admin-bro/express')
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const bcrypt = require('bcrypt');
const bcrypt = require('bcrypt')
const segmentProjectStatusEvents = {
'act': 'activated',
'can': 'deactivated',
'del': 'discarded'
}

// register 3rd party IOC container

Expand Down Expand Up @@ -225,13 +230,22 @@ export async function bootstrap () {
.update<Project>(Project, {status: projectStatus})
.where('project.id IN (:...ids)')
.setParameter('ids', request.query.recordIds.split(','))
.returning("*")
.updateEntity(true)
.execute()

projects.generatedMaps.forEach(project => {
Project.notifySegment(
project,
`Project ${segmentProjectStatusEvents[projectStatus.symbol]}`
)
})
}

} catch (error) {
throw error
}

return {
redirectUrl: 'Project',
records: records.map(record => {
Expand Down Expand Up @@ -344,16 +358,24 @@ export async function bootstrap () {
},
component: false,
},
cancelProject: {
deactivateProject: {
actionType: 'bulk',
isVisible: true,
handler: async (request, response, context) => {
return updateStatuslProjects(context, request, 6)
},
component: false,
},
discardProject: {
actionType: 'bulk',
isVisible: true,
handler: async (request, response, context) => {
return updateStatuslProjects(context, request, 7)
},
component: false,
}
}
}
}
},
{ resource: ProjectStatus, options: {
actions: {
Expand Down
46 changes: 46 additions & 0 deletions services/segment/projectTracker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { User } from '../../entities/user';
import { Project } from '../../entities/project';
import { getAnalytics } from '../../analytics';

const analytics = getAnalytics()

/**
* Notifies Segment any event concerning the project
*/
class ProjectTracker {
project: Project
eventName: string
projectOwner?: User

constructor(projectToUpdate: Project, eventTitle: string) {
this.project = projectToUpdate
this.eventName = eventTitle
}

async track() {
this.projectOwner = await User.findOne({ id: Number(this.project.admin) })

if(this.projectOwner) {
analytics.track(
this.eventName,
this.projectOwner.segmentUserId(),
this.segmentProjectAttributes(),
null
)
}
}

private segmentProjectAttributes() {
return {
email: this.projectOwner?.email,
title: this.project.title,
lastName: this.projectOwner?.lastName,
firstName: this.projectOwner?.firstName,
OwnerId: this.projectOwner?.id,
slug: this.project.slug,
walletAddress: this.project.walletAddress
}
}
}

export default ProjectTracker;

0 comments on commit 08c80e5

Please sign in to comment.