Skip to content

Commit

Permalink
feat: include name with email export
Browse files Browse the repository at this point in the history
  • Loading branch information
alee committed Feb 11, 2022
1 parent 79227e0 commit 370510e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
5 changes: 3 additions & 2 deletions server/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { promisify } from "util";
const mkdir = promisify(fs.mkdir);
const logger = getLogger(__filename);

function getRandomInt(min:number, max:number) {
function getRandomInt(min: number, max: number) {
min = Math.ceil(min);
max = Math.ceil(max);
return Math.floor(Math.random() * (max - min) + min);
Expand Down Expand Up @@ -192,7 +192,8 @@ async function createTournamentRoundInvites(em: EntityManager, tournamentRoundId

async function exportActiveEmails(em: EntityManager, participated: boolean): Promise<void> {
const sp = getServices(em);
const emails = await sp.account.getActiveEmails(participated);
const users = await sp.account.getActiveUsers(participated);
const emails = users.map(u => `${u.name}, ${u.email}`);
fs.writeFile('active-emails.csv', emails.join('\n'), (err) => {
if (err) {
logger.fatal("unable to export active emails: %s", err);
Expand Down
18 changes: 11 additions & 7 deletions server/src/services/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@ export class AccountService extends BaseService {
}

async isEmailAvailable(user: User, email: string): Promise<boolean> {
const otherUser = await this.getRepository().findOne({email});
const otherUser = await this.getRepository().findOne({ email });
if (otherUser) {
return otherUser.id === user.id;
}
return true;
}

async getActiveEmails(participated: boolean): Promise<Array<string>> {
const users: Array<User> = await this.getRepository().find({
async getActiveUsers(participated: boolean): Promise<Array<User>> {
return await this.getRepository().find({
select: ['name', 'email', 'username', 'dateCreated'],
where: { isActive: true, email: Not(IsNull()) },
})
return users.map(u => u.email ?? 'no email specified - should not be possible, check database');
}

async getActiveEmails(participated: boolean): Promise<Array<string>> {
const users: Array<User> = await this.getActiveUsers(participated)
return users.map(u => u.email ?? '');
}

async findUserById(id: number): Promise<User> {
Expand All @@ -56,7 +60,7 @@ export class AccountService extends BaseService {
return user;
}

async getOrCreateUser(username: string, data: Pick<User, 'isBot'> = {isBot: false}): Promise<User> {
async getOrCreateUser(username: string, data: Pick<User, 'isBot'> = { isBot: false }): Promise<User> {
let user = await this.getRepository().findOne({ username });
logger.info('getOrCreateUser for username %s and profile: %o', username);
if (!user) {
Expand All @@ -70,11 +74,11 @@ export class AccountService extends BaseService {
}

async getOrCreateBotUsers(requiredNumberOfBots: number): Promise<Array<User>> {
const bots = await this.getRepository().find({where: {isBot: true}, take: requiredNumberOfBots})
const bots = await this.getRepository().find({ where: { isBot: true }, take: requiredNumberOfBots })
const numberOfBotsToCreate = requiredNumberOfBots - bots.length;
if (numberOfBotsToCreate > 0) {
for (let i = 0; i < numberOfBotsToCreate; i++) {
const user = await this.getOrCreateUser(uuidv4(), {isBot: true});
const user = await this.getOrCreateUser(uuidv4(), { isBot: true });
bots.push(user);
}
}
Expand Down

0 comments on commit 370510e

Please sign in to comment.