🎁 feat: get user and projects from harvest api
This commit is contained in:
parent
2bd3ac7f1a
commit
d6987567c4
6
src/Constants/ErrorMessageInterface.ts
Normal file
6
src/Constants/ErrorMessageInterface.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
interface ErrorMessage {
|
||||||
|
status: string,
|
||||||
|
messages: string[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ErrorMessage
|
14
src/Constants/ErrorMessages.ts
Normal file
14
src/Constants/ErrorMessages.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import ErrorMessage from './ErrorMessageInterface'
|
||||||
|
|
||||||
|
const ErrorMessages: ErrorMessage[] = [
|
||||||
|
{
|
||||||
|
status: 'Err',
|
||||||
|
messages: ['Issue getting User data']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 'Err',
|
||||||
|
messages: ['Issue getting Project data']
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
export default ErrorMessages
|
35
src/Entities/Harvest.ts
Normal file
35
src/Entities/Harvest.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import HarvestInterface from "./Interfaces/HarvestInterface";
|
||||||
|
|
||||||
|
let instance: Harvest | null = null
|
||||||
|
|
||||||
|
class Harvest {
|
||||||
|
accountId: string
|
||||||
|
accessToken: string
|
||||||
|
organization: string
|
||||||
|
|
||||||
|
constructor (props?: HarvestInterface) {
|
||||||
|
if (!instance) instance = this
|
||||||
|
this.accountId = props?.accountId || ''
|
||||||
|
this.accessToken = props?.accessToken || ''
|
||||||
|
this.organization = props?.organization || ''
|
||||||
|
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
|
||||||
|
get headers () {
|
||||||
|
return {
|
||||||
|
'Harvest-Account-ID': this.accountId,
|
||||||
|
'Authorization': `Bearer ${this.accessToken}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get props (): HarvestInterface {
|
||||||
|
return {
|
||||||
|
accountId: this.accountId,
|
||||||
|
accessToken: this.accessToken,
|
||||||
|
organization: this.organization
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Harvest
|
7
src/Entities/Interfaces/HarvestInterface.ts
Normal file
7
src/Entities/Interfaces/HarvestInterface.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
interface HarvestInterface {
|
||||||
|
accountId?: string
|
||||||
|
accessToken?: string
|
||||||
|
organization?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export default HarvestInterface
|
9
src/Entities/Interfaces/ProjectInterface.ts
Normal file
9
src/Entities/Interfaces/ProjectInterface.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import TaskInterface from "./TaskInterface";
|
||||||
|
|
||||||
|
interface ProjectInterface {
|
||||||
|
id: string,
|
||||||
|
name: string,
|
||||||
|
tasks: TaskInterface[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ProjectInterface
|
6
src/Entities/Interfaces/TaskInterface.ts
Normal file
6
src/Entities/Interfaces/TaskInterface.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
interface TaskInterface {
|
||||||
|
id: string,
|
||||||
|
name: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export default TaskInterface
|
9
src/Entities/Interfaces/UserInterface.ts
Normal file
9
src/Entities/Interfaces/UserInterface.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
interface UserInterface {
|
||||||
|
id: number,
|
||||||
|
firstName: string,
|
||||||
|
lastName: string,
|
||||||
|
email: string,
|
||||||
|
avatar: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export default UserInterface
|
24
src/Entities/Project.ts
Normal file
24
src/Entities/Project.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import ProjectInterface from './Interfaces/ProjectInterface'
|
||||||
|
import TaskInterface from './Interfaces/TaskInterface'
|
||||||
|
|
||||||
|
class Project {
|
||||||
|
public id: string
|
||||||
|
public name: string
|
||||||
|
public tasks: TaskInterface[]
|
||||||
|
|
||||||
|
constructor (props: ProjectInterface) {
|
||||||
|
this.id = props.id
|
||||||
|
this.name = props.name
|
||||||
|
this.tasks = props.tasks
|
||||||
|
}
|
||||||
|
|
||||||
|
get props (): ProjectInterface {
|
||||||
|
return {
|
||||||
|
id: this.id,
|
||||||
|
name: this.name,
|
||||||
|
tasks: this.tasks
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Project
|
36
src/Entities/ProjectCollection.ts
Normal file
36
src/Entities/ProjectCollection.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import Project from './Project'
|
||||||
|
|
||||||
|
let instance: ProjectCollection | null = null
|
||||||
|
|
||||||
|
class ProjectCollection {
|
||||||
|
elements: Project[] = []
|
||||||
|
|
||||||
|
constructor () {
|
||||||
|
if (!instance) instance = this
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
|
||||||
|
addOne = (project: Project): void => {
|
||||||
|
this.elements.push(project)
|
||||||
|
}
|
||||||
|
|
||||||
|
addMany = (projects: Project[]): void => {
|
||||||
|
projects.forEach((p: Project) => {
|
||||||
|
this.elements.push(p)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
findById = (id: string): Project | undefined => {
|
||||||
|
const project = this.elements.find((p: Project) => {
|
||||||
|
return p.id === id
|
||||||
|
})
|
||||||
|
return project
|
||||||
|
}
|
||||||
|
|
||||||
|
findByName = (name: string): Project | undefined => {
|
||||||
|
const project = this.elements.find((p: Project) => {
|
||||||
|
return p.name === name
|
||||||
|
})
|
||||||
|
return project
|
||||||
|
}
|
||||||
|
}
|
34
src/Entities/User.ts
Normal file
34
src/Entities/User.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import UserInterface from './Interfaces/UserInterface'
|
||||||
|
|
||||||
|
let instance: User | null
|
||||||
|
|
||||||
|
class User {
|
||||||
|
public readonly id: number
|
||||||
|
public readonly firstName: string
|
||||||
|
public readonly lastName: string
|
||||||
|
public readonly email: string
|
||||||
|
public readonly avatar: string
|
||||||
|
constructor (props: UserInterface) {
|
||||||
|
if (!instance) instance = this
|
||||||
|
|
||||||
|
this.id = props.id
|
||||||
|
this.firstName = props.firstName
|
||||||
|
this.lastName = props.lastName
|
||||||
|
this.email = props.email
|
||||||
|
this.avatar = props.avatar
|
||||||
|
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
|
||||||
|
get props (): UserInterface {
|
||||||
|
return {
|
||||||
|
id: this.id,
|
||||||
|
firstName: this.firstName,
|
||||||
|
lastName: this.lastName,
|
||||||
|
email: this.email,
|
||||||
|
avatar: this.avatar
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default User
|
40
src/UseCases/getProjects.ts
Normal file
40
src/UseCases/getProjects.ts
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import axios from 'axios'
|
||||||
|
import Harvest from '../Entities/Harvest'
|
||||||
|
import ErrorMessage from '../Constants/ErrorMessageInterface'
|
||||||
|
import ErrorMessages from '../Constants/ErrorMessages'
|
||||||
|
import Project from '../Entities/Project'
|
||||||
|
import ProjectInterface from '../Entities/Interfaces/ProjectInterface'
|
||||||
|
|
||||||
|
const getProjects = async (): Promise<Project[] | ErrorMessage> => {
|
||||||
|
const harvest = new Harvest()
|
||||||
|
let projectsResponse: any
|
||||||
|
try {
|
||||||
|
projectsResponse = await axios.get(
|
||||||
|
'https://api.harvestapp.com/v2/users/me/project_assignments',
|
||||||
|
{ headers : harvest.headers }
|
||||||
|
)
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
return ErrorMessages[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
const projectData = projectsResponse.data.project_assignments.map((p: any) => {
|
||||||
|
return {
|
||||||
|
id: p.project.id,
|
||||||
|
name: p.project.name,
|
||||||
|
tasks: p.task_assignments.map((t: any) => {
|
||||||
|
return {
|
||||||
|
id: t.task.id,
|
||||||
|
name: t.task.name
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const projects = projectData.map((p: ProjectInterface) => {
|
||||||
|
return new Project(p)
|
||||||
|
})
|
||||||
|
return projects
|
||||||
|
}
|
||||||
|
|
||||||
|
export default getProjects
|
32
src/UseCases/getUser.ts
Normal file
32
src/UseCases/getUser.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import axios from 'axios'
|
||||||
|
import Harvest from '../Entities/Harvest'
|
||||||
|
import ErrorMessage from '../Constants/ErrorMessageInterface'
|
||||||
|
import ErrorMessages from '../Constants/ErrorMessages'
|
||||||
|
import User from "../Entities/User"
|
||||||
|
|
||||||
|
const getUser = async (): Promise<User | ErrorMessage> => {
|
||||||
|
const harvest = new Harvest()
|
||||||
|
let userResponse: any
|
||||||
|
try {
|
||||||
|
userResponse = await axios.get(
|
||||||
|
'https://api.harvestapp.com/api/v2/users/me.json',
|
||||||
|
{ headers : harvest.headers }
|
||||||
|
)
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
return ErrorMessages[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
const userData = {
|
||||||
|
id: userResponse.data.id || '',
|
||||||
|
firstName: userResponse.data.first_name || '',
|
||||||
|
lastName: userResponse.data.last_name || '',
|
||||||
|
email: userResponse.data.email || '',
|
||||||
|
avatar: userResponse.data.avatar_url || '',
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = new User(userData)
|
||||||
|
return user
|
||||||
|
}
|
||||||
|
|
||||||
|
export default getUser
|
14
src/test/entityTests/HarvestTests.ts
Normal file
14
src/test/entityTests/HarvestTests.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import Harvest from '../../Entities/Harvest'
|
||||||
|
|
||||||
|
const harvest = new Harvest({
|
||||||
|
accountId: '123',
|
||||||
|
accessToken: 'XYZ'
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(harvest.props)
|
||||||
|
|
||||||
|
const harvest2 = new Harvest()
|
||||||
|
|
||||||
|
harvest2.organization = 'ABC'
|
||||||
|
|
||||||
|
console.log(harvest2.props)
|
23
src/test/useCaseTests/getProjectsTests.ts
Normal file
23
src/test/useCaseTests/getProjectsTests.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import ErrorMessage from "../../Constants/ErrorMessageInterface"
|
||||||
|
import Harvest from "../../Entities/Harvest"
|
||||||
|
import Project from "../../Entities/Project"
|
||||||
|
import getProjects from '../../UseCases/getProjects'
|
||||||
|
import env from '../env'
|
||||||
|
|
||||||
|
async function test () {
|
||||||
|
new Harvest({
|
||||||
|
accountId: env.accountId,
|
||||||
|
accessToken: env.accessToken,
|
||||||
|
organization: 'ABC'
|
||||||
|
})
|
||||||
|
|
||||||
|
const projects: Project[] | ErrorMessage = await getProjects()
|
||||||
|
|
||||||
|
if (Array.isArray(projects)) {
|
||||||
|
console.log(projects[0])
|
||||||
|
console.log(`${projects.length} Prpjects found`)
|
||||||
|
}
|
||||||
|
else console.log(projects)
|
||||||
|
}
|
||||||
|
|
||||||
|
test()
|
16
src/test/useCaseTests/getUserTests.ts
Normal file
16
src/test/useCaseTests/getUserTests.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import Harvest from "../../Entities/Harvest"
|
||||||
|
import getUser from '../../UseCases/getUser'
|
||||||
|
import env from '../env'
|
||||||
|
|
||||||
|
async function test () {
|
||||||
|
new Harvest({
|
||||||
|
accountId: env.accountId,
|
||||||
|
accessToken: env.accessToken,
|
||||||
|
organization: 'ABC'
|
||||||
|
})
|
||||||
|
|
||||||
|
const user = await getUser()
|
||||||
|
console.log(user)
|
||||||
|
}
|
||||||
|
|
||||||
|
test()
|
Loading…
x
Reference in New Issue
Block a user