Skip to content
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
41 changes: 41 additions & 0 deletions backend/alembic_migrations/versions/0043_add_contact_to_resumes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""職務経歴書(resumes)に連絡先カラム(email / github_url)を追加する

- resumes に email(メールアドレス・必須運用)と github_url(GitHub URL・任意)を追加。
既存行は連絡先未入力のため server_default="" で後方互換を保つ(次回フォーム保存時に
必須バリデーションが効く)。

libSQL (SQLite 互換) は ADD COLUMN を直接サポートするため upgrade は op.add_column を使う。
downgrade の列削除は、resumes が子テーブル(resume_experiences / resume_qualifications)から
FK 参照される親テーブルのため batch_alter_table(テーブル再作成)を使わない。素のカラムは
SQLite/libSQL 3.35+ の ALTER TABLE DROP COLUMN で直接削除できる。

Revision ID: 0043_add_contact_to_resumes
Revises: 0042_drop_users_hashed_password
Create Date: 2026-06-10 00:00:00.000000
"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "0043_add_contact_to_resumes"
down_revision: Union[str, None] = "0042_drop_users_hashed_password"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
op.add_column(
"resumes",
sa.Column("email", sa.String(length=255), nullable=False, server_default=""),
)
op.add_column(
"resumes",
sa.Column("github_url", sa.String(length=255), nullable=False, server_default=""),
)


def downgrade() -> None:
op.drop_column("resumes", "github_url")
op.drop_column("resumes", "email")
4 changes: 3 additions & 1 deletion backend/app/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
"invalid_input": "入力内容を確認してください。",
"date_range_invalid": "開始日は終了日より前に設定してください。",
"start_date_required": "開始年月を入力してください。",
"end_date_required": "在職中でない場合は終了年月を入力してください。"
"end_date_required": "在職中でない場合は終了年月を入力してください。",
"email_invalid": "メールアドレスの形式が正しくありません。",
"github_url_invalid": "GitHub の URL は https://github.com/ で始まる形式で入力してください。"
},
"server": {
"internal_error": "サーバーエラーが発生しました。しばらくしてから再度お試しください。",
Expand Down
3 changes: 3 additions & 0 deletions backend/app/models/resume.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class Resume(Base):
String(36), ForeignKey("users.id"), nullable=False, index=True
)
full_name: Mapped[str] = mapped_column(String(120), nullable=False, default="")
# 連絡先(メールは必須・GitHub URL は任意)。バリデーションは schemas/resume.py の ResumeBase が正本。
email: Mapped[str] = mapped_column(String(255), nullable=False, default="")
github_url: Mapped[str] = mapped_column(String(255), nullable=False, default="")
career_summary: Mapped[str] = mapped_column(Text, nullable=False, default="")
self_pr: Mapped[str] = mapped_column(Text, nullable=False)
experience_rows: Mapped[list["ResumeExperience"]] = relationship(
Expand Down
2 changes: 2 additions & 0 deletions backend/app/repositories/resume.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class ResumeRepository(SingleUserDocumentRepository):

def _apply_payload(self, entity: Resume, payload: dict[str, object]) -> None:
entity.full_name = payload["full_name"]
entity.email = payload["email"]
entity.github_url = payload.get("github_url", "")
entity.career_summary = payload["career_summary"]
entity.self_pr = payload["self_pr"]
sorted_experiences = sort_by_period_desc(
Expand Down
2 changes: 2 additions & 0 deletions backend/app/routers/resumes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def _resume_to_payload(resume: Resume) -> dict:
"""Resume ORM から PDF/Markdown 生成用 payload を組み立てる。"""
return {
"full_name": resume.full_name,
"email": resume.email,
"github_url": resume.github_url,
"career_summary": resume.career_summary,
"self_pr": resume.self_pr,
"experiences": resume.experiences,
Expand Down
36 changes: 35 additions & 1 deletion backend/app/schemas/resume.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@
``Client.vacation_*``(期間・詳細)を使う。
"""

import re
from datetime import datetime
from typing import Literal
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field, field_serializer, model_validator
from pydantic import (
BaseModel,
ConfigDict,
Field,
field_serializer,
field_validator,
model_validator,
)

from ..core.date_utils import to_jst
from ..core.messages import get_error
Expand Down Expand Up @@ -219,13 +227,39 @@ def validate_dates(self) -> "Experience":
return self


# 簡易メール形式(RFC 5322 完全準拠ではなく UX 優先の最小チェック)。FE 側 payloadBuilders と一致させる。
_EMAIL_PATTERN = re.compile(r"^[^\s@]+@[^\s@]+\.[^\s@]+$")
# GitHub アカウント URL の接頭辞。値があるときのみ前方一致で検証する。
_GITHUB_URL_PREFIX = "https://github.com/"


class ResumeBase(BaseModel):
full_name: str = Field(min_length=1, max_length=120)
email: str = Field(min_length=1, max_length=255)
github_url: str = Field(default="", max_length=255)
career_summary: str = Field(min_length=1, max_length=2000)
self_pr: str = Field(min_length=1, max_length=2000)
experiences: list[Experience] = Field(default_factory=list)
qualifications: list[ResumeQualificationItem] = Field(default_factory=list)

@field_validator("email")
@classmethod
def validate_email(cls, value: str) -> str:
"""メールアドレスの簡易形式チェック。前後空白を除去して正規化し、不正なら 422(日本語)で返す。"""
trimmed = value.strip()
if not _EMAIL_PATTERN.match(trimmed):
raise ValueError(get_error("validation.email_invalid"))
return trimmed

@field_validator("github_url")
@classmethod
def validate_github_url(cls, value: str) -> str:
"""GitHub URL は任意。値があるときだけ ``https://github.com/`` 始まりを要求する。前後空白は除去する。"""
stripped = value.strip()
if stripped and not stripped.startswith(_GITHUB_URL_PREFIX):
raise ValueError(get_error("validation.github_url_invalid"))
return stripped


class ResumeCreate(ResumeBase):
pass
Expand Down
9 changes: 9 additions & 0 deletions backend/app/services/markdown/generators/resume_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ def build_resume_markdown(payload: dict[str, Any]) -> str:
full_name = payload.get("full_name", "")
if full_name:
lines.append(field_line("氏名", full_name))
# 連絡先(メールは必須・GitHub URL は任意。値があるときだけ行を出す)
email = payload.get("email", "")
if email:
lines.append(field_line("email", email))
github_url = payload.get("github_url", "")
if github_url:
# github URL は Markdown リンク記法でハイパーリンク化する(GitHub/各種ビューアで
# 青・下線のリンク表示になる)。href は schema で https://github.com/ 始まりに検証済み。
lines.append(field_line("github", f"[{github_url}]({github_url})"))
lines.append("")

qualifications = payload.get("qualifications", [])
Expand Down
18 changes: 18 additions & 0 deletions backend/app/services/pdf/generators/resume_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,24 @@ def _build_html(resume: dict) -> str:
f'<div class="meta">氏名 <span data-fp="full_name">{_esc(full_name)}</span></div>',
)

# 連絡先(メールは必須・GitHub URL は任意。値があるときだけ行を出す)
email = resume.get("email") or ""
if email:
parts.append(
f'<div class="meta">email <span data-fp="email">{_esc(email)}</span></div>',
)
github_url = resume.get("github_url") or ""
if github_url:
# github URL はハイパーリンク表示(青・下線)にする。href は schema で
# https://github.com/ 始まりに検証済みのため安全。data-fp は左右 diff の
# ハイライト対象として span ではなく <a> に直接付与する(annotateHtml は
# [data-fp] 全要素を対象にするため要素種別は問わない)。
esc_url = _esc(github_url)
parts.append(
f'<div class="meta">github '
f'<a class="meta-link" href="{esc_url}" data-fp="github_url">{esc_url}</a></div>',
)

# 記載日(日本時間)
today = datetime.now(JST)
parts.append(
Expand Down
6 changes: 6 additions & 0 deletions backend/app/services/pdf/templates/resume.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ h1 {
margin-bottom: 4mm;
}

/* 連絡先の github URL をハイパーリンク表示(青・下線)にする。 */
.meta-link {
color: #0066cc;
text-decoration: underline;
}

h2 {
font-size: 12pt;
border-bottom: 2px solid #333;
Expand Down
1 change: 1 addition & 0 deletions backend/tests/auth/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def test_me_returns_current_user(client) -> None:
def _csrf_resume_payload() -> dict:
return {
"full_name": "テスト",
"email": "test@example.com",
"career_summary": "要約",
"self_pr": "自己PR",
"experiences": [],
Expand Down
1 change: 1 addition & 0 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def make_resume_payload(**overrides) -> dict:
"""
payload: dict = {
"full_name": "山田 太郎",
"email": "yamada@example.com",
"career_summary": "キャリアサマリー",
"self_pr": "自己PR",
"experiences": [
Expand Down
1 change: 1 addition & 0 deletions backend/tests/security/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

RESUME_PAYLOAD: dict = {
"full_name": "山田 太郎",
"email": "yamada@example.com",
"career_summary": "キャリアサマリー",
"self_pr": "自己PR",
"experiences": [],
Expand Down
45 changes: 44 additions & 1 deletion backend/tests/test_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from fastapi.testclient import TestClient

from conftest import auth_header
from conftest import auth_header, make_resume_payload

# ── CRUD: Auth required (401 without token) ────────────────────

Expand Down Expand Up @@ -30,6 +30,7 @@ def test_resume_crud(client: TestClient) -> None:
"/api/resumes",
json={
"full_name": "田中太郎",
"email": "tanaka@example.com",
"career_summary": "キャリアサマリー",
"self_pr": "自己PR",
"experiences": [],
Expand All @@ -50,6 +51,7 @@ def test_resume_crud(client: TestClient) -> None:
f"/api/resumes/{resume_id}",
json={
"full_name": "田中花子",
"email": "tanaka@example.com",
"career_summary": "更新済みサマリー",
"self_pr": "自己PR",
"experiences": [],
Expand All @@ -62,6 +64,40 @@ def test_resume_crud(client: TestClient) -> None:
assert resp.json()["full_name"] == "田中花子"


def test_resume_contact_round_trips(client: TestClient) -> None:
"""メール・GitHub URL が作成→取得で往復することを確認する。"""
headers = auth_header(client, "resume-contact-user")
payload = make_resume_payload(
full_name="連絡先 太郎",
email="contact@example.com",
github_url="https://github.com/contact-taro",
)
resp = client.post("/api/resumes", json=payload, headers=headers)
assert resp.status_code == 201, resp.text

resp = client.get("/api/resumes/latest", headers=headers)
assert resp.status_code == 200
body = resp.json()
assert body["email"] == "contact@example.com"
assert body["github_url"] == "https://github.com/contact-taro"


def test_resume_rejects_invalid_email_via_api(client: TestClient) -> None:
"""不正なメール形式は 422 で拒否される。"""
headers = auth_header(client, "resume-bademail-user")
payload = make_resume_payload(email="not-an-email")
resp = client.post("/api/resumes", json=payload, headers=headers)
assert resp.status_code == 422


def test_resume_rejects_invalid_github_url_via_api(client: TestClient) -> None:
"""github.com 以外の GitHub URL は 422 で拒否される。"""
headers = auth_header(client, "resume-badgithub-user")
payload = make_resume_payload(github_url="https://gitlab.com/taro")
resp = client.post("/api/resumes", json=payload, headers=headers)
assert resp.status_code == 422


def test_resume_qualifications_sorted_asc(client: TestClient) -> None:
"""資格一覧が取得日昇順で返ることを確認する。"""
headers = auth_header(client, "resume-sort-user")
Expand All @@ -70,6 +106,7 @@ def test_resume_qualifications_sorted_asc(client: TestClient) -> None:
"/api/resumes",
json={
"full_name": "ソート確認",
"email": "sort@example.com",
"career_summary": "要約",
"self_pr": "自己PR",
"experiences": [],
Expand Down Expand Up @@ -97,6 +134,7 @@ def test_resume_qualification_date_is_year_month(client: TestClient) -> None:
"/api/resumes",
json={
"full_name": "年月確認",
"email": "ym@example.com",
"career_summary": "要約",
"self_pr": "自己PR",
"experiences": [],
Expand All @@ -119,6 +157,7 @@ def test_resume_round_trips_nested_structure(client: TestClient) -> None:

payload = {
"full_name": "山田 太郎",
"email": "yamada@example.com",
"career_summary": "キャリアサマリー",
"self_pr": "自己PR",
"experiences": [
Expand Down Expand Up @@ -179,6 +218,7 @@ def test_resume_round_trips_non_it_and_vacation(client: TestClient) -> None:

payload = {
"full_name": "佐藤 花子",
"email": "sato@example.com",
"career_summary": "キャリアサマリー",
"self_pr": "自己PR",
"experiences": [
Expand Down Expand Up @@ -284,6 +324,7 @@ def test_resume_get_by_id(client: TestClient) -> None:
"/api/resumes",
json={
"full_name": "山田 太郎",
"email": "yamada@example.com",
"career_summary": "サマリー",
"self_pr": "自己PR",
"experiences": [],
Expand Down Expand Up @@ -318,6 +359,7 @@ def test_resume_preview_returns_annotated_html(client: TestClient) -> None:
"/api/resumes/preview",
json={
"full_name": "山田太郎",
"email": "yamada@example.com",
"career_summary": "キャリアサマリー",
"self_pr": "自己PR",
"experiences": [
Expand Down Expand Up @@ -374,6 +416,7 @@ def test_resume_preview_validation_error(client: TestClient) -> None:
"/api/resumes/preview",
json={
"full_name": "",
"email": "yamada@example.com",
"career_summary": "x",
"self_pr": "y",
"experiences": [],
Expand Down
36 changes: 36 additions & 0 deletions backend/tests/test_markdown_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,42 @@ def test_capital_unit_defaults_to_sen_man_when_missing() -> None:
assert "5千万円" in md


def test_contact_fields_rendered_when_present() -> None:
md = build_resume_markdown(
{
"full_name": "山田 太郎",
"email": "yamada@example.com",
"github_url": "https://github.com/yamada",
"career_summary": "職務要約",
"self_pr": "自己PR",
"qualifications": [],
"experiences": [],
}
)
assert "yamada@example.com" in md
# ラベルは email / github(小文字)。
assert "**email:**" in md
assert "**github:**" in md
# github URL は Markdown リンク記法でハイパーリンク化する。
assert "[https://github.com/yamada](https://github.com/yamada)" in md


def test_contact_fields_omitted_when_empty() -> None:
# github URL は任意。空なら github 行を出さない(email は必須運用だが空入力にも耐える)。
md = build_resume_markdown(
{
"full_name": "山田 太郎",
"email": "yamada@example.com",
"github_url": "",
"career_summary": "職務要約",
"self_pr": "自己PR",
"qualifications": [],
"experiences": [],
}
)
assert "**github:**" not in md


def _it_experience() -> dict:
return {
"company": "Example株式会社",
Expand Down
Loading
Loading