Skip to content

Commit

Permalink
[add] Formal Member verification of Vote Ticket creating
Browse files Browse the repository at this point in the history
Signed-off-by: TechQuery <shiy2008@gmail.com>
  • Loading branch information
TechQuery committed Jan 20, 2025
1 parent a12466c commit 255b202
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/controller/Election.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {
Authorized,
Body,
CurrentUser,
ForbiddenError,
JsonController,
Param,
Post
} from 'routing-controllers';
import { ResponseSchema } from 'routing-controllers-openapi';
import { makeSHA } from 'web-utility';

import { dataSource, User, Voter, VoteTicket } from '../model';
import { lark, MemberBiDataTable } from '../utility';
import { ActivityLogController } from './ActivityLog';

@JsonController('/election')
Expand All @@ -21,21 +22,31 @@ export class ElectionController {
@ResponseSchema(VoteTicket)
async createVoteTicket(
@CurrentUser() createdBy: User,
@Body() { electionName }: Voter
@Param('electionName') electionName: string
): Promise<VoteTicket> {
const { id, nickName, mobilePhone, email } = createdBy;

const duplicatedVoter = await this.voterStore.findOneBy({
createdBy,
createdBy: { id },
electionName
});
if (duplicatedVoter)
throw new ForbiddenError(
`${createdBy.nickName} has already registered for ${electionName}`
`${nickName} has already registered for ${electionName} election`
);
const { nickName, mobilePhone, email } = createdBy;
await lark.getAccessToken();

const { id } = await this.voterStore.save({ electionName, createdBy });
const [formalMember] = await new MemberBiDataTable().getList({
手机号: mobilePhone,
formalMember: true
});
if (!formalMember)
throw new ForbiddenError(
`${nickName} isn't a formal member who has the right to vote in ${electionName} election`
);
const saved = await this.voterStore.save({ electionName, createdBy });

await ActivityLogController.logCreate(createdBy, 'Voter', id);
await ActivityLogController.logCreate(createdBy, 'Voter', saved.id);

const meta = [
nickName,
Expand Down
9 changes: 9 additions & 0 deletions src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
BiDataQueryOptions,
BiDataTable,
LarkApp,
makeSimpleFilter,
TableCellLink,
TableCellValue,
TableRecordFields
} from 'mobx-lark';
import { Filter } from 'mobx-restful';
import { HttpError } from 'routing-controllers';
import { FindOptionsWhere, ILike } from 'typeorm';

Expand Down Expand Up @@ -78,6 +80,7 @@ export class CommonBiDataTable extends BiDataTable<TableRecordFields>() {
export interface Person extends Record<'name' | 'gender' | '手机号', string> {
email: TableCellLink;
avatar: TableCellValue;
formalMember: boolean;
}

export class PersonBiDataTable extends BiDataTable<Person>() {
Expand All @@ -89,6 +92,12 @@ export class PersonBiDataTable extends BiDataTable<Person>() {
}
}

export class MemberBiDataTable extends PersonBiDataTable {
makeFilter(filter: Filter<Person>) {
return makeSimpleFilter(filter, '=');
}
}

export const blobURLOf = (value: TableCellValue) =>
value instanceof Array
? typeof value[0] === 'object' &&
Expand Down

0 comments on commit 255b202

Please sign in to comment.