Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API]: @scoffs/refactor/backend #189

Merged
merged 8 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@
},
"dependencies": {
"@elysiajs/cors": "^1.0.2",
"elysia": "^1.0.10",
"elysia": "^1.0.13",
"elysia-compression": "^0.0.7",
"elysia-helmet": "^1.0.2",
"pg": "^8.11.3",
"pg": "^8.11.5",
"pg-hstore": "^2.3.4",
"rand-token": "^1.0.1",
"sequelize": "^6.37.2",
"@diary-spo/crypto": "workspace:*",
"cache-manager": "^5.4.0",
"cache-manager": "^5.5.1",
"sequelize-simple-cache": "^1.3.5",
"node-rsa": "^1.1.1"
},
"devDependencies": {
"@types/node-rsa": "^1.1.4",
"@diary-spo/shared": "workspace:*",
"@elysiajs/swagger": "^1.0.3",
"@types/pg": "~8.11.2"
"@types/pg": "^8.11.5"
}
}
30 changes: 30 additions & 0 deletions apps/api/src/ApiError/ApiError.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { API_CODES } from './types'

export class ApiError extends Error {
code: number
message: string
Expand All @@ -9,3 +11,31 @@ export class ApiError extends Error {
Object.setPrototypeOf(this, ApiError.prototype)
}
}

export class NotFoundError extends ApiError {
constructor(message: string) {
super(message, API_CODES.NOT_FOUND)
Object.setPrototypeOf(this, NotFoundError.prototype)
}
}

export class UnauthorizedError extends ApiError {
constructor(message: string) {
super(message, API_CODES.UNAUTHORIZED)
Object.setPrototypeOf(this, UnauthorizedError.prototype)
}
}

export class ForbiddenError extends ApiError {
constructor(message: string) {
super(message, API_CODES.FORBIDDEN)
Object.setPrototypeOf(this, ForbiddenError.prototype)
}
}

export class UnknownError extends ApiError {
constructor(message: string) {
super(message, API_CODES.UNKNOWN_ERROR)
Object.setPrototypeOf(this, UnknownError.prototype)
}
}
10 changes: 0 additions & 10 deletions apps/api/src/LogError/index.ts

This file was deleted.

5 changes: 4 additions & 1 deletion apps/api/src/models/Absence/model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { cache, enableCache, sequelize } from '@db'
import { DataTypes } from 'sequelize'

import { cache, enableCache, sequelize } from '@db'

import { AbsenceTypeModel } from '../AbsenceType'
import { DiaryUserModel } from '../DiaryUser'
import { ScheduleModel } from '../Schedule'

import type { IModelPrototype } from '../types'

export type AbsenceModelType = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AbsenceTypesKeys } from '@diary-spo/shared'
import { AbsenceTypeModel, type IAbsenceTypeModel } from '@models'

export const saveOrGetAbsenceType = async (
export const absenceTypeSaveOrGet = async (
name: AbsenceTypesKeys
): Promise<IAbsenceTypeModel> =>
AbsenceTypeModel.findOrCreate({
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/models/AbsenceType/actions/save/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './saveOrGetAbsenceType'
export * from './absenceTypeSaveOrGet'
4 changes: 3 additions & 1 deletion apps/api/src/models/AbsenceType/model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { cache, enableCache, sequelize } from '@db'
import { AbsenceTypes, type AbsenceTypesKeys } from '@diary-spo/shared'
import { DataTypes } from 'sequelize'

import { cache, enableCache, sequelize } from '@db'

import type { IModelPrototype } from '../types'

export type AbsenceTypeModelType = {
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/models/AcademicYear/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './model'
//export * from './actions'
export * from './actions'
4 changes: 3 additions & 1 deletion apps/api/src/models/AcademicYear/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { sequelize } from '@db'
import { DataTypes } from 'sequelize'

import { sequelize } from '@db'

import { GroupModel } from '../Group'
import { TermTypeModel } from '../TermType'
import type { IModelPrototype } from '../types'
Expand Down
5 changes: 2 additions & 3 deletions apps/api/src/models/Ads/actions/get/adsGetFromDB.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ICacheData } from '@helpers'
import { AdsModel } from '@models'

export const adsGetFromDB = async (authData: ICacheData) => {
return AdsModel.findAll({
export const adsGetFromDB = async (authData: ICacheData) =>
AdsModel.findAll({
where: {
spoId: authData.spoId
},
Expand All @@ -12,4 +12,3 @@ export const adsGetFromDB = async (authData: ICacheData) => {
},
order: [['date', 'DESC']]
})
}
8 changes: 2 additions & 6 deletions apps/api/src/models/Ads/actions/save/saveAd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@ import type { NotificationsResponse } from '@diary-spo/shared'
import { type ICacheData, retriesForError } from '@helpers'
import { adSaveOrGer } from './'

export const saveAd = async (
ad: NotificationsResponse,
authData: ICacheData
) => {
return retriesForError(adSaveOrGer, [ad, authData])
}
export const saveAd = async (ad: NotificationsResponse, authData: ICacheData) =>
retriesForError(adSaveOrGer, [ad, authData])
4 changes: 3 additions & 1 deletion apps/api/src/models/Ads/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { sequelize } from '@db'
import { DataTypes } from 'sequelize'

import { sequelize } from '@db'

import { SPOModel } from '../SPO'
import type { IModelPrototype } from '../types'

Expand Down
4 changes: 3 additions & 1 deletion apps/api/src/models/Auth/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { sequelize } from '@db'
import { DataTypes } from 'sequelize'

import { sequelize } from '@db'

import { DiaryUserModel } from '../DiaryUser'
import type { IModelPrototypeNoId } from '../types'

Expand Down
5 changes: 4 additions & 1 deletion apps/api/src/models/Classroom/model.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { cache, enableCache, sequelize } from '@db'
import { DataTypes } from 'sequelize'

import { cache, enableCache, sequelize } from '@db'

import { SPOModel } from '../SPO'
import type { IModelPrototype } from '../types'

// REMOVE IT
// ?
export type ClassroomModelType = {
id: bigint
building: string
Expand Down
7 changes: 3 additions & 4 deletions apps/api/src/models/DiaryUser/actions/get/getUserById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import {

export type IUserInfo = DiaryUserModelType & { group?: GroupModelType }

// не используется нигде
export const getUserById = async (
id: bigint,
getGroup = false
): Promise<IUserInfo | null> => {
// TODO: fix it
return DiaryUserModel.findOne({
): Promise<IUserInfo | null> =>
DiaryUserModel.findOne({
where: {
id
},
include: getGroup ? GroupModel : undefined
})
}
3 changes: 3 additions & 0 deletions apps/api/src/models/DiaryUser/actions/get/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './getUserById'

export * from './saveOrGetDiaryUser'
export * from './saveOrGetGroup'
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
type IDiaryUserModel
} from '@models'
import type { Optional } from 'sequelize'
// ВЫНЕСТИ В МОДЕЛЬ юзера

export const saveOrGetDiaryUser = async (
data: Optional<DiaryUserModelType, 'id'>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { GroupModel, type IGroupModel } from '@models'
// ВЫНЕСТИ В МОДЕЛЬ группы

export const saveOrGetGroup = async (
id: number,
Expand Down
7 changes: 5 additions & 2 deletions apps/api/src/models/DiaryUser/model.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { Nullable } from '@diary-spo/shared'
import { DataTypes } from 'sequelize'

import { KEY } from '@config'
import { sequelize } from '@db'
import type { Nullable } from '@diary-spo/shared'
import { formatDate } from '@utils'
import { DataTypes } from 'sequelize'

import { GroupModel } from '../Group'
import type { IModelPrototype } from '../types'

// REMOVE IT
// ?
export type DiaryUserModelType = {
id: bigint
groupId: bigint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { ExaminationKeys } from '@diary-spo/shared'
import { ExaminationTypeModel } from '../../model'

export const saveOrGetExaminationType = async (type: ExaminationKeys) => {
return ExaminationTypeModel.findOrCreate({
export const saveOrGetExaminationType = async (type: ExaminationKeys) =>
ExaminationTypeModel.findOrCreate({
where: {
name: type
},
defaults: {
name: type
}
}).then((v) => v[0])
}
4 changes: 3 additions & 1 deletion apps/api/src/models/Examination/model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { cache, enableCache, sequelize } from '@db'
import { type ExaminationKeys, Examinations } from '@diary-spo/shared'
import { DataTypes } from 'sequelize'

import { cache, enableCache, sequelize } from '@db'

import type { IModelPrototype } from '../types'

export type ExaminationTypeModelType = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ export const saveOrGetFinalMark = async (
subjectId: bigint,
authData: ICacheData
) => {
// why
const where = {
subjectId,
diaryUserId: authData.localUserId
}
return FinalMarkModel.findOrCreate({
// why
where,
defaults: {
// why
...where,
markValueId
}
Expand Down
4 changes: 3 additions & 1 deletion apps/api/src/models/FinalMark/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { sequelize } from '@db'
import { DataTypes } from 'sequelize'

import { sequelize } from '@db'

import { DiaryUserModel } from '../DiaryUser'
import { MarkValueModel } from '../MarkValue'
import { SubjectModel } from '../Subject'
Expand Down
4 changes: 3 additions & 1 deletion apps/api/src/models/Group/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { sequelize } from '@db'
import { DataTypes } from 'sequelize'

import { sequelize } from '@db'

import { SPOModel } from '../SPO'
import type { IModelPrototype } from '../types'

Expand Down
4 changes: 3 additions & 1 deletion apps/api/src/models/LessonType/model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { cache, enableCache, sequelize } from '@db'
import { LessonType, type LessonTypeKeys } from '@diary-spo/shared'
import { DataTypes } from 'sequelize'

import { cache, enableCache, sequelize } from '@db'

import type { IModelPrototype } from '../types'

export type LessonTypeModelType = {
Expand Down
3 changes: 1 addition & 2 deletions apps/api/src/models/Mark/actions/delete/markDelete.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { ICacheData } from '@helpers'
import { MarkModel } from '@models'

export const markDelete = async (taskId: bigint, authData: ICacheData) => {
export const markDelete = async (taskId: bigint, authData: ICacheData) =>
MarkModel.destroy({
where: {
taskId,
diaryUserId: authData.localUserId
}
})
}
3 changes: 1 addition & 2 deletions apps/api/src/models/Mark/actions/save/markSaveOrGet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { MarkKeys } from '@diary-spo/shared'
import { type ICacheData, retriesForError } from '@helpers'
import { type IScheduleModel, markValueSaveOrGet } from '@models'
import { formatDate } from '@utils'
import { markValueSaveOrGet } from 'src/models/MarkValue'
import type { IScheduleModel } from 'src/models/Schedule'
import { markSaveOrGetUnSafe } from './markSaveOrGetUnSafe'

export const markSaveOrGet = async (
Expand Down
4 changes: 3 additions & 1 deletion apps/api/src/models/Mark/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { sequelize } from '@db'
import { DataTypes } from 'sequelize'

import { sequelize } from '@db'

import { DiaryUserModel } from '../DiaryUser'
import { MarkValueModel } from '../MarkValue'
import { TaskModel } from '../Task'
Expand Down
4 changes: 3 additions & 1 deletion apps/api/src/models/MarkValue/model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { cache, enableCache, sequelize } from '@db'
import { Grade, type MarkKeys } from '@diary-spo/shared'
import { DataTypes } from 'sequelize'

import { cache, enableCache, sequelize } from '@db'

import type { IModelPrototype } from '../types'

export type MarkValueModelType = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const requiredSaveOrGet = async (
taskId,
diaryUserId: authData.localUserId
}

return RequiredModel.findOrCreate({
where,
defaults: {
Expand Down
4 changes: 3 additions & 1 deletion apps/api/src/models/Required/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { sequelize } from '@db'
import { DataTypes } from 'sequelize'

import { sequelize } from '@db'

import { DiaryUserModel } from '../DiaryUser'
import { TaskModel } from '../Task'
import type { IModelPrototypeNoId } from '../types'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type ISPOModel, SPOModel, type SPOModelType } from '@models'
import type { Optional } from 'sequelize'
// ВЫНЕСТИ В МОДЕЛЬ СПО

export const saveOrGetSPO = async (
data: Optional<SPOModelType, 'id'>
): Promise<ISPOModel> => {
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/models/SPO/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './model'
export * from './actions/saveOrGetSPO'
4 changes: 3 additions & 1 deletion apps/api/src/models/SPO/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { sequelize } from '@db'
import { DataTypes } from 'sequelize'

import { sequelize } from '@db'

import type { IModelPrototype } from '../types'

export type SPOModelType = {
Expand Down
Loading
Loading