draft-redmine_users_from_csv/import_csv_users_group.ts

41 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-01-15 20:46:54 +01:00
import { Redmine } from 'bluemine';
type CsvEntry = {
firstname: string;
lastname: string;
mail: string;
};
async function addUsersToGroup(redmine: Redmine, csv: CsvEntry[]) {
const users: { id: number; login: string }[] = [];
for (const { firstname, lastname } of csv) {
const login = `${firstname.toLowerCase()}.${lastname.toLowerCase()}`;
try {
const list = await redmine.users.list({ limit: 1, name: login });
const user = list.users[0];
console.log(`get user: ${user.login}#${user.id}`);
users.push(user);
} catch {
console.error(`error list: ${login}`);
}
}
const { groups } = await redmine.unstableGroups.list() as unknown as {
groups: { id: number; name: string }[];
};
const { id } = groups.filter(({ name }) => name === 'entrepreneur-01-24')[0];
for (const user of users) {
try {
console.error(`ok: ${user.login}#${user.id}`);
await redmine.unstableGroups.addUser(id, user);
} catch {
console.error(`error: ${user.login}`);
}
}
2024-01-15 20:46:54 +01:00
}