Skip to content

Commit

Permalink
feat(products): create models for products crud
Browse files Browse the repository at this point in the history
  • Loading branch information
AnakUtara committed Jul 12, 2024
1 parent 3c80577 commit 62a112d
Show file tree
Hide file tree
Showing 17 changed files with 208 additions and 21 deletions.
3 changes: 2 additions & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"license": "ISC",
"dependencies": {
"@prisma/client": "^5.16.1",
"nodemon": "^3.1.4",
"axios": "^1.7.2",
"bcrypt": "^5.1.1",
"cors": "^2.8.5",
Expand All @@ -29,8 +28,10 @@
"jsonwebtoken": "^9.0.2",
"multer": "^1.4.5-lts.1",
"mustache": "^4.2.0",
"nanoid": "^5.0.7",
"node-cron": "^3.0.3",
"nodemailer": "^6.9.13",
"nodemon": "^3.1.4",
"sharp": "^0.33.4",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
Expand Down
32 changes: 16 additions & 16 deletions apps/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ model User {
voucher Promotion? @relation(fields: [voucher_id], references: [id])
is_banned Boolean @default(false)
customer_orders CustomerOrders[]
Cart Cart[]
cart Cart[]
order_detail OrderDetail[]
created_at DateTime @default(now())
updated_at DateTime @updatedAt
OrderDetail OrderDetail[]
@@index([id, email, role, is_verified])
@@map("users")
Expand Down Expand Up @@ -141,7 +141,7 @@ model StoreStock {
promo Promotion? @relation(fields: [promo_id], references: [id])
quantity Int
order_details OrderDetail[]
Cart Cart[]
cart Cart[]
created_at DateTime @default(now())
updated_at DateTime @updatedAt
Expand Down Expand Up @@ -174,15 +174,15 @@ enum ImageType {
}

model Image {
id String @id @default(cuid())
name String @unique
blob Bytes @db.MediumBlob
type ImageType @default(product)
user User?
ProductVariants ProductVariants?
category Category?
created_at DateTime @default(now())
updated_at DateTime @updatedAt
id String @id @default(cuid())
name String @unique
blob Bytes @db.MediumBlob
type ImageType @default(product)
user User?
product_variants ProductVariants?
category Category?
created_at DateTime @default(now())
updated_at DateTime @updatedAt
@@index([type])
@@map("images")
Expand Down Expand Up @@ -275,9 +275,9 @@ model Promotion {
min_transaction Float @db.Double
expiry_date DateTime
is_valid Boolean
StoreStock StoreStock[]
CustomerOrders CustomerOrders[]
User User[]
store_stock StoreStock[]
customer_orders CustomerOrders[]
user User[]
created_at DateTime @default(now())
updated_at DateTime @updatedAt
Expand Down Expand Up @@ -321,7 +321,7 @@ model CustomerOrders {
promotion Promotion? @relation(fields: [promotion_id], references: [id])
discount Int?
user_id String?
User User? @relation(fields: [user_id], references: [id])
user User? @relation(fields: [user_id], references: [id])
origin_id String
origin Store @relation(fields: [origin_id], references: [address_id])
order_details OrderDetail[]
Expand Down
16 changes: 16 additions & 0 deletions apps/api/src/constants/image.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { generateSlug } from '@/utils/generate';
import { Prisma } from '@prisma/client';
import sharp from 'sharp';

export const imageCreateInput = async (
file: Express.Multer.File,
id: string,
): Promise<Prisma.ImageCreateInput> => {
const blob = await sharp(file.buffer).webp().toBuffer();
const name = `${generateSlug(file.fieldname)}-${id}`;
return {
name,
blob,
type: 'avatar',
};
};
File renamed without changes.
18 changes: 18 additions & 0 deletions apps/api/src/libs/prisma/images.args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { generateSlug } from '@/utils/generate';
import { ImageType, Prisma } from '@prisma/client';
import { Request } from 'express';
import sharp from 'sharp';

export async function imageCreate(
req: Request,
id: string,
mimetype: 'png' | 'jpeg' | 'webp' | 'gif',
type: ImageType,
): Promise<Prisma.ImageCreateInput> {
const file = req.file as Express.Multer.File;
return {
name: `${generateSlug(file.fieldname)}-${id}`,
blob: await sharp(file.buffer)[`${mimetype}`]().toBuffer(),
type,
};
}
File renamed without changes.
2 changes: 1 addition & 1 deletion apps/api/src/middlewares/admin.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { TAddress } from '@/models/address.model';
import { TUser } from '@/models/user.model';
import prisma from '@/prisma';
import { AuthError, BadRequestError, InvalidDataError } from '@/utils/error';
import { adminFindFirst } from '@/utils/prisma/user.args';
import { adminFindFirst } from '@/libs/prisma/user.args';
import { reqBodyReducer } from '@/utils/req.body.helper';
import { Role } from '@prisma/client';
import { compare } from 'bcrypt';
Expand Down
23 changes: 23 additions & 0 deletions apps/api/src/models/category.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TImage } from './image.model';
import { TProduct } from './products.model';

export type TCategory = {
id: number;
name: string;
image_id?: string;
image?: TImage;
product: TProduct[];
sub_categories: TSubCategory[];
created_at?: Date;
updated_at?: Date;
};

export type TSubCategory = {
id?: number;
name: string;
category_id: number;
category: TCategory;
products: TProduct[];
created_at?: Date;
updated_at?: Date;
};
25 changes: 25 additions & 0 deletions apps/api/src/models/image.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { TCategory } from './category.model';
import { TVariant } from './products.model';
import { TUser } from './user.model';

export enum ImageType {
avatar = 'avatar',
store = 'store',
product = 'product',
promotion = 'promotion',
discount = 'discount',
voucher = 'voucher',
category = 'category',
}

export type TImage = {
id?: string;
name?: string;
blob: Buffer;
type: ImageType;
user?: TUser;
ProductVariants: TVariant;
category?: TCategory;
created_at?: Date;
updated_at?: Date;
};
8 changes: 7 additions & 1 deletion apps/api/src/models/products.model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { TCategory, TSubCategory } from './category.model';
import { TImage } from './image.model';
import { TStoreStock } from './store.model';

export type TProduct = {
id?: string;
name: string;
Expand All @@ -7,7 +11,9 @@ export type TProduct = {
storage_instructions?: string;
category_id: number;
sub_category_id: number;
variants: TVariant[];
category?: TCategory;
sub_category?: TSubCategory;
variants?: TVariant[];
created_at?: Date;
updated_at?: Date;
};
Expand Down
26 changes: 26 additions & 0 deletions apps/api/src/models/promo.models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { TStoreStock } from './store.model';
import { TUser } from './user.model';

export enum PromoType {
discount = 'discount',
voucher = 'voucher',
referral_voucher = 'referral_voucher',
cashback = 'cashback',
free_shipping = 'free_shipping',
}

export type TPromotion = {
id?: string;
title: string;
description?: string;
type: PromoType;
amount: number;
min_transaction: number;
expiry_date: Date;
is_valid: Boolean;
store_stock: TStoreStock[];
// CustomerOrders CustomerOrders[]
user?: TUser[];
created_at?: Date;
updated_at?: Date;
};
32 changes: 32 additions & 0 deletions apps/api/src/models/store.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { TAddress } from './address.model';
import { TVariant } from './products.model';
import { TPromotion } from './promo.models';
import { TUser } from './user.model';

export type TStore = {
address_id: string;
address: TAddress;
store_admin: TUser[];
product_stock: TStoreStock[];
schedule_id?: string;
// schedule?: TStoreSchedule
// customer_orders: TCustomerOrders[]
created_at?: Date;
updated_at?: Date;
};

export type TStoreStock = {
id: string;
store_id: string;
store: TStore;
variant_id: string;
product: TVariant;
// stock_history: TStockHistory[];
unit_price: number;
discount?: number;
promo_id?: string;
promo?: TPromotion;
quantity: number;
created_at?: Date;
updated_at?: Date;
};
2 changes: 1 addition & 1 deletion apps/api/src/services/admin.auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createToken } from '@/libs/jwt';
import { TUser } from '@/models/user.model';
import prisma from '@/prisma';
import { catchAllErrors } from '@/utils/error';
import { adminOmit } from '@/utils/prisma/user.args';
import { adminOmit } from '@/libs/prisma/user.args';
import { Request } from 'express';

class AdminAuthService {
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/services/categories.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class CategoryService {}

export default new CategoryService();
2 changes: 1 addition & 1 deletion apps/api/src/services/super-admin.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TUser } from '@/models/user.model';
import prisma from '@/prisma';
import { catchAllErrors, NotFoundError } from '@/utils/error';
import { countTotalPage, paginate } from '@/utils/pagination';
import { userFindMany } from '@/utils/prisma/user.args';
import { userFindMany } from '@/libs/prisma/user.args';
import { Address, AddressType, Role, User } from '@prisma/client';
import { Request } from 'express';

Expand Down
18 changes: 18 additions & 0 deletions apps/api/src/utils/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { generate } from 'voucher-code-generator';

export const generateReferral = () => {
const referral = generate({
pattern: '####-####',
count: 1,
charset: 'alphanumeric',
});

return referral[0];
};

export const generateSlug = (str: string): string => {
return str
.toLocaleLowerCase()
.replace(/ /g, '-')
.replace(/[^\w-]+/g, '');
};
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 62a112d

Please sign in to comment.