Skip to content

Instantly share code, notes, and snippets.

@tudorilisoi
Last active May 23, 2019 14:54
Show Gist options
  • Save tudorilisoi/d1cbc83a730161f1680ae2af61201ca4 to your computer and use it in GitHub Desktop.
Save tudorilisoi/d1cbc83a730161f1680ae2af61201ca4 to your computer and use it in GitHub Desktop.
// https://www.npmjs.com/package/faker
const faker = require('faker')
faker.seed(123);
const { parse, stringify } = require('flatted/cjs');
function unique(fn, arr, objKey) {
const value = fn()
const exists = arr.find(item => item[objKey] === value)
if (exists !== undefined) {
return unique(fn, arr, objKey)
}
return value
}
const roles = [
{ id: 1, title: 'Admin' },
{ id: 2, title: 'Client' },
{ id: 3, title: 'Contractor' },
{ id: 4, title: 'Project Manager' },
]
const users = []
for (let i = 0; i < 100; i++) {
const randomRole = roles[Math.floor(Math.random() * roles.length)]
const user = {
//fields
id: i,
email: unique(faker.internet.email, users, 'email'),
username: unique(faker.internet.userName, users, 'username'),
full_name: faker.name.findName(),
phone: faker.internet.phone,
//associated objects
role: randomRole, //the role Object corresponding to the role_id
projects: [],
}
users.push(user)
}
const projects = []
for (let i = 0; i < 100; i++) {
const user1 = users[Math.floor(Math.random() * users.length)]
const user2 = users[Math.floor(Math.random() * users.length)]
const project = {
//fields
id: i,
title: unique(faker.internet.userName, projects, 'username'),
//associated objects
client: null, //relation based on client_id
contractors: [user1, user2],
managers: [user1, user2],
}
user1.projects = [project]
user2.projects = [project]
projects.push(project)
}
// console.log(users)
console.log(stringify(projects))
//fake API
function geProjects(options = {
dateStart:null,
dateEnd:null,
status:null,
}) {
let ret = [...projects]
if(options.dateStart){
ret = ret.filter(user=>dayjs().isAfter()) //TODO fix this
}
return Promise.resolve(ret)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment