draft-redmine_users_from_csv/src/append_to_group.ts

40 lines
1,007 B
TypeScript

import { Redmine } from 'bluemine'
import { CsvEntry } from '../type.ts'
import { log, toLogin } from '../utils.ts'
async function addUsersToGroup(
redmine: Redmine,
csv: CsvEntry[],
groupName: string,
) {
const users: Redmine['users']['List']['Result']['users'] = []
for (const { firstname, lastname } of csv) {
const login = toLogin(firstname, lastname)
try {
const list = await redmine.users.list({ limit: 1, name: login })
const user = list.users[0]
log.info(`get id of ${user.login}#${user.id}`)
users.push(user)
} catch (error) {
log.error(`get id of ${login}`)
console.error(String(error))
}
}
const { groups } = await redmine.unstableGroups.list()
const { id } = groups.filter(({ name }) => name === 'entrepreneur-01-24')[0]
for (const user of users) {
try {
log.info(`add ${user.login}#${user.id} to "${groupName}"`)
await redmine.unstableGroups.addUser(id, user)
} catch {
log.error(`add ${user.login}#${user.id} to "${groupName}"`)
}
}
}