---
# System prepended metadata

title: 後端程式簡介

---

# 後端程式簡介
- [ ] 
- [ ] basedata
- [ ] bsbackend
- [ ] case
- [ ] chat
- [ ] g_tools
- [x] mailing
- [x] notification
- [x] point
- [x] supportweb->匯出fb
- [x] templates
- [ ] tests
- [x] transactions
- [x] upload
- [ ] user
- [ ] utils

---


## Note 要測試必須有三個角色
1. student:學生
2. teacher:教師
3. qualified_teacher:配合教師

1. 寫程式創建使用者
2. 寫程式操作使用者

## Cases
### app
#### admin
CaseAdmin

#### adminfilter
CaseFilter
CaseOwnerFilter
TeacherFilterForLog
QualifiedFilter
#### app
import signal
- cases.signals
- cases.feedback.signals
- cases.pricing.signals
- cases.relatedfile.signals


#### constant
STATUS_CHOICES
MIMETYPES
DEFAULT_MIMETYPES
#### factory
CaseFactory->創建case實例
包含：
- case_no
- ended_at
- caseType
- @factory.post_generation
	- docType
	- factory
#### form
CaseModelForm
檢測 status在什麼階段,會要什麼參數,沒有的話報錯
#### models
Case
- case_no
- case_name
- topic
- subject
- description
- memo
- cost
- predicted_price
- case_owner-> 案主
- teacher-> 接案者
- qualified_teacher-> 合作老師
- is_urgent
- status
- caseType
- docType
- root_folder_id->google 資料夾ID
- ended_at
- paid

Record:(include time and stats)
- case
- review_deadline->審核截止
- review_at->審核日期
- finish_review_at->完成審核日期
- negotiate_deadline->接案截止
- choose_at->接案日期
- operating_deadline->作業截止
- first_upload_at->完成日期(老師第一次上傳)
- finished_at->結案日期
- cancel_at->取消日期
- avg_pricing->平均報價

Pricing:
- case
- teacher->報價教師
- predicted_price
- description

#### serializers
RecordSerializer
CaseListSerializer->案件瀏覽
CaseSerializer->單一案件
CaseCreateSerializer->案件刊登
#### signals
##### post case save
update_user_record->案件完成後更新使用者紀錄
case_update_sheet->呼叫更新google sheet 的task
case_create_drive_folder->呼叫創建google drive folder 的task
case_review->調用deadline_check task
case_negotiate->調用deadline_check task
case_operating->?
case_auto_finish->在案件ended_at後72小時自動結束
case_canceled->當案件取消後自動更新紀錄
case_paid
case_refund->取消案件退款
case_closed_allocation->案件完結分配
create_main_chatroom->建立主要聊天室
chatroom_participants->邀請所有聊天室成員
chatroom_archived->聊天室封存
- NEGOTIATE(不儲存)
- OPERATING(儲存)
- 封存
	- CLOSED
	- TERMINATED
	- CANCELLED
#### tasks(?)
deadline_check
auto_finish
backup_to_gdrive
case_create_drive_folder
case_update_sheet
#### urls
case
- /
- /reviewing
- /review
- /me
#### utils
##### ansir generate info
gen_caseno
gen_casename
gen_subject_string(substring(科目的總和 ex. 科目1,科目2,科目3))
##### member
get_all_members->回傳`[case_owner,teacher,qualified_teacher]`
##### deadline
check_review_datetime->回傳review時間(min(創建時間,結束時間))
check_negotiate_datetime->回傳協商時間(min(review_deadline+3天,結束時間))
check_operating_datetime->回傳運作時間
##### file
parse_mimetype->傳入檔名,回傳mimi type
##### url
generate_frontend_case_url->傳入案件,回傳案件前端地址
generate_gdrive_url->傳入案件,回傳案件google drive地址
#### views
@method_decorator->裝飾下面的class
CaseViewSet
ReviewViewSet->審核區案件(給老師看的)
ReviewingViewSet->我審核的案件
CaseMineViewSet->我的發問
### feedback
#### admin
FeedbackAdmin
#### constant
FEEDBACK_ID
FEEDBACK_DIRECTIONS
#### form
FeedbackForm
#### models
##### Feedback->案件回饋
- case
- subject ? case 不是已經有科目了嗎
- reviewer
- reviewed
- score(1-5)
- comment
- reviewer_type->回饋者類型
- reviewed_type->回饋對象類型
- updated_count->修改次數
- 儲存時的規則
	- 不能review自己
	- unique field combination
#### serializers
FeedbackSerializer
#### signals
update_user_record
#### urls
feedback
#### views
FeedbackViewSet->案件回饋
包含回饋權限和條件
### pricing
#### admin
PricingAdmin
##### functions
- chosen_one->選定的報價
- open_pricing->仍可報價
- choose_teacher->接受此報價
#### forms
PricingModelForm
#### serializers
UserBaseSerializer->顯示使用者的平均被評分(avg_feedback_score)
TeacherForPricing
PricingSerializer->案件報價序列化器
#### signals
update_case_record->更新平均價格
#### urls
me->PricingMineViewSet
/->PricingViewSet
#### utils
accept_pricing->接受報價
- 更變case的狀態->產稱transaction
#### views
PricingViewSet->案件報價
PricingMineViewSet->我的報價
### relatedfile
#### admin
FileInline->給case admin用
FileAdmin
#### forms
FileModelForm->給 FileAdmin用
#### models
RelatedFile:
- case
- title
- description
- file
- private
- get_upload_path()
#### serializers
RelatedFileSerializer
#### signals
backup_to_gdrive->排程,呼叫cases.tasks.backup_to_gdrive
#### utils
create_teacher_file->給老師上傳解答用
## Transaction
### app
#### admin
TransactionAdmin
新東西：
list_filter
autocomplete_fields
search_fields
使用form在admin上
重寫`save_model`並在更改status時會自動寄信

#### apps
TransactionConfig
使用warpper來封裝
#### constant
TX_STATUS->交易狀態
TX_TYPE->交易類型
TX_FAIL_TYPE->失敗類型
TOTAL_RATIO = 1.15
TAX_RATIO = 1.05
FIRST_REFERER_RATIO = 0.10
LATER_REFERER_RATIO = 0.02
TEACHER_RATIO = 0.70
QUALIFIED_TEACHER_RATIO = 0.10
#### forms
TransactionForm
傳入的交易資料+驗證邏輯
#### models
###### Transaction->交易紀錄
fields:
- id
- user->關係人
- account->帳戶
- case->案件
- amount->交易金額
- fee->手續費
- type->類型
- title->標題
- description->敘述
- status->狀態
- failed_type->失敗類型

functions:
- _pay->支出
- _paid->收入
- _withdraw->提領
- _refund-> 退款
- _get_processing_withdrawn_amount->取得待審核或作業中的交易紀錄
- _get_prev_this_month_withdrawn_count->取得這筆之前當月提領次數
- _get_this_month_other_withdrawn_count->取得當月其他筆提領數量
- _update_this_month_other_record_fee->取得當月其他筆紀錄費用
- _check_account_belonging->檢查此交易使用者是否正確
- _check_balance
- _check_withdraw_balance->檢查申請的金額是否超過仍可使用的提領
- _check_taxdata_required->檢查是否需要填寫稅務資料
- _check_case_personnel->檢查案件是否可以被當前使用者所使用
- save->儲存交易紀錄(包含交易邏輯)
#### serializers
- TransactionSerializer
- AggregateIncomeSerializer
	- gen_prev_date
	- get_results
- ChartSummarySerializer->為了swagger
#### urls
transaction
#### utils
pay->Ansir pay
pay_from_ecpay->Ecpay
paid->創建付款給老師交易紀錄
refund->創建退費交易紀錄
withdraw_apply->創建退款交易紀錄
#### views
TransactionViewset(ListOnlyViewSet)->交易紀錄
### ecpay(綠界支付)
#### app
##### sdk
綠界付款sdk
##### serializers
ECPayCreateOrderSerializer
->validate_pid
##### urls
id_generator->產生id
create_order_no->產生自訂訂單編號
get_ecpay_sdk
##### wrapper
##### utils
##### wrapper
###### ECPaySdkWrapper
action_url
sdk_params
default_order_params->不會變動的訂單參數
atm_order_params
cvs_order_params
barcode_order_params
credit_order_params
generate_order_params
pay
check_mac_value
#### invoice
##### admin
EcpayInvoiceInline
只能看其他都不能做
##### constants
EcpayInvoiceURL
CARRUER_TYPE
##### models
EcpayInvoice
- id
- order
- identifier
- name
- address
- email
- phone
- print
- donation
- lovecode
- carrier_type
- carrier_number

##### serializers
ECPayInvoiceSerializer
##### utils
主要四個功能
開立、搜尋、重開、作廢
_action_success->判斷電子發票回傳狀態是否成功
_process_ecpay_response->處理 Ecpay 回應
_trasfer_safe->把null轉換成空字串
_transfer_bool->把bool轉換成字串"1","0"
_create_basic_invoice->建立有基本資訊的發票
settle->離線開立發票
search_invoice->搜尋發票
search_invoice_number->搜尋發票號碼
void->作廢發票，用於退款
resettle->重開發票
#### order
訂單(綠界付款資料)
##### admin
EcpayOrderAdmin
只有查詢的權限
##### constants
會使用到的常數,包含：
- 字軌類別
- 課稅類別
- 銀聯卡交易選項
- 付款方式(信用卡,網路 ATM)
- 子付款方式(銀行 ex.台新銀行,玉山銀行)

##### model
EcpayOrder
- id
- tx
- order_no
- payment_type
- subpayment_type
- payment_info
- payment_result

##### serializers
ECPayOrderSerializer
##### urls
- payment_notify
- payment_info
##### utils
expire_processing_payment_from_ecpay:舊付款紀錄過期
tx.ecpayorder.delete()->?
##### views
payment_info->檢查付款資訊
payment_hook->付款結果
## User
### app
#### admin
PersonalInline->個人資訊
UserAdmin
#### adminfilter
UserFilter(AutocompleteFilter)
#### constant
ViolationType->違規類型
#### factory
UserFactory
#### field
RefererField
#### filters
BalanceFilter->是否有餘額
TaxDataFilter->是否有填勞報資訊
#### forms
UserForm
#### models
User(AbstractUser):
- username
- id
- avatar
- email
- email_verified
- phone
- phone_verified
- third_registration->第三方註冊
- referer->推薦人

Personal->個人資料:
- user
- aboutme
- school
- department
- enter_school_year
- gender
- city
- town
- address
- facebook_response
- google_response
- line_response
- instagram_response


#### serializers
- UserBaseSerializer
- TeacherForCase
- UserInfoSerializer
- UpdatePwdSerializer->更新密碼
- PersonalSerializer
	- 集成
		- RefererField
		- UserInfoSerializer
		- TeacherSerializer
		- AccountSerializer
		- TaxDataSerializer
		- WalletSerializer
		- ViolationSerializer
		- RecordSerializer
	- Method
		- get_pwd_required->是否不需要密碼
		- to_internal_value->申請老師
		- update->更新個人資訊
			- username
			- email
			- phone
		- custom_validate_phone
		- custom_validate_email
		- update_foreign_table
- PersonalProfileSerializer
	- 集成
		- UserInfoSerializer
		- TeacherSerializer
- djoser api
	- Signup
	- CustomActivationSerializer
	- CustomResetPasswordSerializer
#### signals
post user save:
- create_user_wallet
- create_user_info
- create_user_record

pre account save:
- clean_default
#### urls
referer
account
taxdata
message-template

#### utils
get_superuser
get_full_address->使用者詳細地址

#### validations
validate_id_number->身分證驗證
CustomUsernameValidator->使用者名稱驗證
#### views
UserViewSet->Override djoser ViewSet
overwrite method:
- get_serializer_class
- get_permissions
- perform_create->Override activation email settings
- activation->Override for email verified field
- resend_activation->Override for email verified field
- add_password->第三方新增密碼
- teacher_apply->老師申請
- personal_profile->個人公開頁面
### chat
### money
### phone
### record
### referer
### social_auth
### teacher
### voilation