-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
769 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './model' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { DataTypes } from 'sequelize' | ||
|
||
import { cache, enableCache, sequelize } from '@db' | ||
|
||
import type {IModelPrototype} from '../types' | ||
import {Nullable} from "@diary-spo/shared"; | ||
import {DiaryUserModel} from "../DiaryUser"; | ||
|
||
export type BalanceOperationModelType = { | ||
id: bigint | ||
diaryUserId: bigint | ||
senderId: Nullable<bigint> | ||
comment: Nullable<string> | ||
amount: number | ||
datetime?: string | ||
} | ||
|
||
export type IBalanceOperationModelType = IModelPrototype<BalanceOperationModelType, 'id'> | ||
|
||
const balanceOperationModel = sequelize.define<IBalanceOperationModelType>('balanceOperation', { | ||
id: { | ||
type: DataTypes.BIGINT, | ||
primaryKey: true, | ||
autoIncrement: true | ||
}, | ||
senderId: { | ||
type: DataTypes.BIGINT, | ||
allowNull: true, | ||
references: { | ||
model: DiaryUserModel | ||
} | ||
}, | ||
diaryUserId: { | ||
type: DataTypes.BIGINT, | ||
references: { | ||
model: DiaryUserModel | ||
} | ||
}, | ||
comment: { | ||
type: DataTypes.STRING, | ||
allowNull: true | ||
}, | ||
amount: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false | ||
}, | ||
datetime: { | ||
type: DataTypes.DATEONLY, | ||
allowNull: false, | ||
defaultValue: Date.now() | ||
} | ||
}) | ||
|
||
export const BalanceOperationModel = enableCache | ||
? cache.init<IBalanceOperationModelType>(balanceOperationModel) | ||
: balanceOperationModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
apps/api/src/routes/marketAvatars/index.ts → apps/api/src/routes/market/avatars/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import {BalanceOperationModel} from "../../../models/BalanceOperation"; | ||
import {AvatarModel} from "../../../models/Avatar"; | ||
import {API_CODES, API_ERRORS, ApiError} from "@api"; | ||
import {UserAvatarModel} from "../../../models/UserAvatar"; | ||
|
||
type OperationStatus = { | ||
isSuccess: boolean | ||
currentBalance: number | ||
} | ||
|
||
// TODO: ВЫНЕСТИ ВСЁ КРАСИВЕНЬКО ТУТ И ТАМ | ||
|
||
const buyAvatar = async (localUserId: bigint, strAvatarId: string): Promise<OperationStatus> => { | ||
const avatarId = BigInt(strAvatarId) | ||
const currentBalance = await BalanceOperationModel.sum('amount', {where: {diaryUserId: localUserId}}) | ||
const avatar = await AvatarModel.findOne({where: {id: avatarId}}) | ||
|
||
if (!avatar) | ||
throw new ApiError(API_ERRORS.DATA_NOT_FOUND, API_CODES.NOT_FOUND) | ||
|
||
if (currentBalance < avatar.price) | ||
return { | ||
isSuccess: false, | ||
currentBalance | ||
} | ||
|
||
const userAvatarAlready = await UserAvatarModel.findOne({ | ||
where: { | ||
diaryUserId: localUserId, | ||
avatarId | ||
} | ||
}) | ||
|
||
if (userAvatarAlready) | ||
return { | ||
isSuccess: false, | ||
currentBalance | ||
} | ||
|
||
await BalanceOperationModel.create({ | ||
diaryUserId: localUserId, | ||
comment: `Покупка ${avatar.isAnimated ? 'анимированного' : 'статичного'} аватара (${avatar.id})`, | ||
amount: -avatar.price | ||
}) | ||
|
||
await UserAvatarModel.create({ | ||
diaryUserId: localUserId, | ||
avatarId | ||
}) | ||
|
||
return { | ||
isSuccess: true, | ||
currentBalance | ||
} | ||
} | ||
|
||
export default buyAvatar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Elysia, t } from 'elysia' | ||
import { AuthPlugin } from '../../../services/AuthService' | ||
import buyAvatar from './handler' | ||
|
||
export const BuyAvatar = new Elysia() | ||
.use(AuthPlugin) | ||
.post('/buyAvatar', ({ | ||
Auth: { | ||
user: {localUserId} | ||
}, body: { | ||
avatarId | ||
}}) => buyAvatar(localUserId, avatarId), { | ||
detail: { | ||
tags: ['market'] | ||
}, | ||
body: t.Object({ | ||
avatarId: t.String() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './avatars' | ||
export * from './userInfo' | ||
export * from './userAvatars' | ||
export * from './userSaveAvatar' | ||
export * from './buy' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { AvatarData } from '@diary-spo/shared' | ||
import {IUserAvatarModelType, UserAvatarModel} from "../../../models/UserAvatar"; | ||
import {AvatarModel, IAvatarModelType} from "../../../models/Avatar"; | ||
import {DiaryUserModel} from "../../../models/DiaryUser"; | ||
|
||
type IAvatarsFromDB = IUserAvatarModelType & { | ||
avatar: IAvatarModelType | ||
} | ||
|
||
type UserAvatarData = AvatarData & {isActive: boolean} | ||
|
||
const getUserAvatars = async (localUserId: bigint): Promise<UserAvatarData[]> => { | ||
const currentUserAvatar = (await DiaryUserModel.findByPk(localUserId))?.avatarId ?? null | ||
const userAvatars = (await UserAvatarModel.findAll({ | ||
where: { | ||
diaryUserId: localUserId, | ||
}, | ||
include: { | ||
model: AvatarModel | ||
} | ||
})) as IAvatarsFromDB[] | ||
|
||
const formattedResult: UserAvatarData[] = [] | ||
|
||
for (const userAvatar of userAvatars) | ||
formattedResult.push({ | ||
id: userAvatar.avatar.id, | ||
filename: userAvatar.avatar.filename, | ||
tags: [], | ||
isAnimated: userAvatar.avatar.isAnimated, | ||
price: userAvatar.avatar.price, | ||
isActive: currentUserAvatar === userAvatar.avatarId | ||
}) | ||
|
||
return formattedResult | ||
} | ||
|
||
export default getUserAvatars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Elysia } from 'elysia' | ||
import { AuthPlugin } from '../../../services/AuthService' | ||
import getUserAvatars from './handler' | ||
|
||
export const UserAvatars = new Elysia() | ||
.use(AuthPlugin) | ||
.get('/userAvatars', ({Auth: {user: {localUserId}}}) => getUserAvatars(localUserId), { | ||
detail: { | ||
tags: ['user'] | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {DiaryUserModel} from "../../../models/DiaryUser"; | ||
import {API_CODES, API_ERRORS, ApiError} from "@api"; | ||
import {AvatarModel} from "../../../models/Avatar"; | ||
import {Nullable} from "@diary-spo/shared"; | ||
import {BalanceOperationModel} from "../../../models/BalanceOperation"; | ||
|
||
type MarketUserInfo = { | ||
firstName: string | ||
lastName: string | ||
avatar: Nullable<string> | ||
balance: number | ||
} | ||
|
||
const getUserInfo = async (localUserId: bigint): Promise<MarketUserInfo> => { | ||
const diaryUser = await DiaryUserModel.findByPk(localUserId) | ||
|
||
if (!diaryUser) | ||
throw new ApiError(API_ERRORS.USER_NOT_FOUND, API_CODES.NOT_FOUND) | ||
|
||
const firstName = diaryUser.firstName | ||
const lastName = diaryUser.lastName | ||
|
||
const avatarModel = diaryUser.avatarId ? | ||
await AvatarModel.findByPk(diaryUser.avatarId) | ||
: null | ||
|
||
const avatar = avatarModel ? avatarModel.filename : null | ||
|
||
// Если нет записей, то может вернуться что-то другое (вдруг), поэтому ставим ?? | ||
const balance = await BalanceOperationModel.sum('amount', {where: {diaryUserId: diaryUser.id}}) ?? 0 | ||
|
||
return { | ||
firstName, | ||
lastName, | ||
avatar, | ||
balance | ||
} | ||
} | ||
|
||
export default getUserInfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Elysia } from 'elysia' | ||
import { AuthPlugin } from '../../../services/AuthService' | ||
import getUserInfo from './handler' | ||
|
||
export const UserInfo = new Elysia() | ||
.use(AuthPlugin) | ||
.get('/userInfo', ({ | ||
Auth: { | ||
user: {localUserId} | ||
}}) => getUserInfo(localUserId), { | ||
detail: { | ||
tags: ['market'] | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {Nullable} from "@diary-spo/shared" | ||
import {UserAvatarModel} from "../../../models/UserAvatar"; | ||
import {API_CODES, API_ERRORS, ApiError} from "@api"; | ||
import {DiaryUserModel} from "../../../models/DiaryUser"; | ||
|
||
type MarketUserInfo = { | ||
firstName: string | ||
lastName: string | ||
avatar: Nullable<string> | ||
balance: number | ||
} | ||
|
||
const userSaveAvatar = async (localUserId: bigint, stringAvatarId: string): Promise<void> => { | ||
const avatarId = BigInt(stringAvatarId) | ||
const avatarIsSetNull = avatarId === BigInt(-1) | ||
|
||
if (!avatarIsSetNull) { | ||
const userAvatar = await UserAvatarModel.findOne({where: {avatarId}}) | ||
|
||
if (!userAvatar) | ||
throw new ApiError(API_ERRORS.DATA_NOT_FOUND, API_CODES.NOT_FOUND) | ||
} | ||
|
||
await DiaryUserModel.update({ | ||
avatarId: avatarIsSetNull ? null : avatarId | ||
}, | ||
{ | ||
where: { | ||
id: localUserId | ||
} | ||
}) | ||
} | ||
|
||
export default userSaveAvatar |
Oops, something went wrong.