Skip to content

Commit

Permalink
Merge pull request #61 from academic-relations/60-impl-post-apis-for-…
Browse files Browse the repository at this point in the history
…organization-features-apiorg003-to-009

60 impl post apis for organization features apiorg003 to 005
  • Loading branch information
Gerbera3090 authored Nov 28, 2024
2 parents 3093421 + 7f7af90 commit 5cf5313
Show file tree
Hide file tree
Showing 24 changed files with 988 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Body, Controller, Get, Param, Post, UsePipes } from "@nestjs/common";
import {
Body,
Controller,
Get,
Param,
Post,
Put,
UsePipes,
} from "@nestjs/common";

import { ZodPipe } from "@sparcs-students/api/common/pipes/zod-pipe";
import {
Expand All @@ -9,7 +17,23 @@ import {
ApiOrg002RequestUrl,
apiOrg002,
ApiOrg002RequestBody,
ApiOrg002ResponseOK,
ApiOrg002ResponseCreated,
ApiOrg003RequestUrl,
apiOrg003,
ApiOrg003RequestBody,
ApiOrg003ResponseCreated,
ApiOrg004RequestUrl,
apiOrg004,
ApiOrg004ResponseOK,
ApiOrg004RequestBody,
ApiOrg005RequestBody,
ApiOrg005ResponseCreated,
ApiOrg005RequestUrl,
apiOrg005,
ApiOrg006RequestBody,
ApiOrg006ResponseCreated,
apiOrg006,
ApiOrg006RequestUrl,
} from "@sparcs-students/interface/api/organization/index";

import { OrganizationService } from "../service/organization.service";
Expand All @@ -30,8 +54,45 @@ export class OrganizationController {
@UsePipes(new ZodPipe(apiOrg002))
async postOrganization(
@Body() body: ApiOrg002RequestBody,
): Promise<ApiOrg002ResponseOK> {
): Promise<ApiOrg002ResponseCreated> {
const res = await this.organizationService.postOrganization(body);
return res;
}

@Post(ApiOrg003RequestUrl)
@UsePipes(new ZodPipe(apiOrg003))
async postOrganizationPresident(
@Body() body: ApiOrg003RequestBody,
): Promise<ApiOrg003ResponseCreated> {
const res = await this.organizationService.postOrganizationPresident(body);
return res;
}

@Put(ApiOrg004RequestUrl)
@UsePipes(new ZodPipe(apiOrg004))
async putOrganizationPresidentRetire(
@Body() body: ApiOrg004RequestBody,
): Promise<ApiOrg004ResponseOK> {
const res =
await this.organizationService.putOrganizationPresidentRetire(body);
return res;
}

@Post(ApiOrg005RequestUrl)
@UsePipes(new ZodPipe(apiOrg005))
async postOrganizationMember(
@Body() body: ApiOrg005RequestBody,
): Promise<ApiOrg005ResponseCreated> {
const res = await this.organizationService.postOrganizationMember(body);
return res;
}

@Post(ApiOrg006RequestUrl)
@UsePipes(new ZodPipe(apiOrg006))
async postOrganizationManager(
@Body() body: ApiOrg006RequestBody,
): Promise<ApiOrg006ResponseCreated> {
const res = await this.organizationService.postOrganizationManager(body);
return res;
}
}
3 changes: 2 additions & 1 deletion packages/api/src/feature/organization/organization.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Module } from "@nestjs/common";

import { DrizzleModule } from "src/drizzle/drizzle.module";
import { SemesterModule } from "src/feature/semester/semester.module";
import { UserModule } from "src/feature/user/user.module";
import { OrganizationService } from "./service/organization.service";
import { OrganizationController } from "./controller/organization.controller";
import { OrganizationPublicService } from "./service/organization.public.service";
import { OrganizationRepository } from "./repository/organization.repository";

@Module({
imports: [DrizzleModule, SemesterModule],
imports: [DrizzleModule, SemesterModule, UserModule],
controllers: [OrganizationController],
providers: [
OrganizationService,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { Injectable, Inject } from "@nestjs/common";
import { ApiOrg002RequestBody } from "@sparcs-students/interface/api/organization/index";
import { and, or, lte, gte, eq, isNull } from "drizzle-orm";
import logger from "@sparcs-students/api/common/util/logger";

import {
ApiOrg002RequestBody,
ApiOrg003RequestBody,
ApiOrg005RequestBody,
ApiOrg006RequestBody,
} from "@sparcs-students/interface/api/organization/index";

import { and, or, lte, gte, eq, isNull, desc } from "drizzle-orm";
import { MySql2Database } from "drizzle-orm/mysql2";
import { DrizzleAsyncProvider } from "src/drizzle/drizzle.provider";

Expand All @@ -17,6 +25,9 @@ import {
UserStudentT,
TeamT,
Team,
OrganizationMember,
OrganizationManager,
OrganizationMemberT,
} from "src/drizzle/schema";

export type OrganizationWithPresidentT = {
Expand Down Expand Up @@ -154,4 +165,186 @@ export class OrganizationRepository {
const res = await this.ckOrganizationBeforeCreate(body);
return res;
}

async ckOrganizationPresidentBeforeCreate(
body: Omit<ApiOrg003RequestBody, "ignorePrev">,
): Promise<number> {
const select = await this.db
.select()
.from(OrganizationPresident)
.where(
and(
eq(OrganizationPresident.organizationId, body.organizationId),
eq(
OrganizationPresident.organizationPresidentTypeEnumId,
body.organizationPresidentTypeE,
),

isNull(OrganizationPresident.endTerm),
),
)
.limit(1)
.orderBy(desc(OrganizationPresident.createdAt));
if (select.length === 0) {
return 0;
}
return select[0].id;
}

async updateOrganizationPresidentRetire(
organizationPresidentId: number,
endTerm: Date,
): Promise<number> {
await this.db
.update(OrganizationPresident)
.set({ endTerm })
.where(eq(OrganizationPresident.id, organizationPresidentId))
.execute();

const resSelect = await this.db
.select()
.from(OrganizationPresident)
.where(eq(OrganizationPresident.id, organizationPresidentId));
if (resSelect.length === 0 || resSelect[0].id !== organizationPresidentId) {
return 0;
}
return resSelect[0].id;
}

async createOrganizationPresident(
body: Omit<ApiOrg003RequestBody, "ignorePrev">,
): Promise<number> {
await this.db
.insert(OrganizationPresident)
.values({
organizationId: body.organizationId,
userId: body.userId,
organizationPresidentTypeEnumId: body.organizationPresidentTypeE,
phoneNumber: body.phoneNumber,
startTerm: body.startTerm,
endTerm: body.endTerm ? body.endTerm : null,
})
.execute();

const res = await this.ckOrganizationPresidentBeforeCreate(body);
return res;
}

async ckOrganizationPresidentAlready(userId: number): Promise<number> {
// TODO: 지금은 공직일 때만 체크하는 로직이 없는데, 언젠가는 추가해야 함
const select = await this.db
.select()
.from(OrganizationPresident)
.where(
and(
eq(OrganizationPresident.userId, userId),
isNull(OrganizationPresident.endTerm),
),
)
.limit(1);
if (select.length === 0) {
// TODO: 해당 president가 공직이 아닐 경우 그냥 0을 리턴하는 로직을 추가해야 함.
return 0;
}
return select[0].id;
}

async selectOrganizationPresidentById(
organizationPresidentId: number,
): Promise<OrganizationPresidentT[]> {
const res = this.db
.select()
.from(OrganizationPresident)
.where(and(eq(OrganizationPresident.id, organizationPresidentId)));
return res;
}

async ckOrganizationMemberBeforeCreate(
body: ApiOrg005RequestBody,
): Promise<number> {
const res = await this.db
.select()
.from(OrganizationMember)
.where(
and(
eq(OrganizationMember.organizationId, body.organizationId),
eq(OrganizationMember.userId, body.userId),
isNull(OrganizationMember.endTerm),
),
)
.orderBy(desc(OrganizationMember.createdAt))
.limit(1);
logger.info(res);
if (res.length === 0) {
return 0;
}
return res[0].id;
}

async createOrganizationMember(body: ApiOrg005RequestBody): Promise<number> {
await this.db
.insert(OrganizationMember)
.values({
organizationId: body.organizationId,
userId: body.userId,
startTerm: body.startTerm,
endTerm: body.endTerm ? body.endTerm : null,
})
.execute();

const res = await this.ckOrganizationMemberBeforeCreate(body);
return res;
}

async selectOrganizationMemberByUserIdAndOrganizationId(
userId: number,
organizationId: number,
): Promise<OrganizationMemberT[]> {
const res = await this.db
.select()
.from(OrganizationMember)
.where(
and(
eq(OrganizationMember.userId, userId),
eq(OrganizationMember.organizationId, organizationId),
isNull(OrganizationMember.endTerm),
),
);
return res;
}

async ckOrganizationManagerBeforeCreate(
body: ApiOrg006RequestBody,
): Promise<number> {
const res = await this.db
.select()
.from(OrganizationManager)
.where(
and(
eq(OrganizationManager.organizationId, body.organizationId),
eq(OrganizationManager.userId, body.userId),
eq(OrganizationManager.semesterId, body.semesterId),
),
)
.orderBy(desc(OrganizationManager.createdAt))
.limit(1);
if (res.length === 0) {
return 0;
}
return res[0].id;
}

async createOrganizationManager(body: ApiOrg006RequestBody): Promise<number> {
await this.db
.insert(OrganizationManager)
.values({
organizationId: body.organizationId,
userId: body.userId,
semesterId: body.semesterId,
})
.execute();

const res = await this.ckOrganizationManagerBeforeCreate(body);
return res;
}
}
Loading

0 comments on commit 5cf5313

Please sign in to comment.